我正在学习 XAML,但在将控件的内容绑定(bind)到子窗口的设置属性时遇到了问题。
这是我为了更清楚地说明而制作的一个简单示例。从主窗口调用 child :
Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
Dim OwndWindow As New WindowChild
OwndWindow.Owner = Me
OwndWindow.ShowDialog()
End Sub
这是 child :
<Window x:Class="WindowChild"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication2"
Title="WindowChild" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider x:Key="ObjDatPro" ObjectType="{x:Type src:TestSettings}"></ObjectDataProvider>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource ObjDatPro}}">
<CheckBox Content="CheckBox" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center"
IsChecked="{Binding Default.BoolSetting, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
此时 Visual Studio 报告错误:命名空间“clr-namespace:WpfApplication2”中不存在名称“TestSettings”。(第 7 行,第 49 列)
我已经尝试将这部分更改为 WindowChild.TestSettings
,但随后 VS 提示不支持嵌套类型。
将 CLR 命名空间更改为 WpfApplication2.WindowChild
甚至 WindowChild
都没有用,VS 说:未定义的 CLR 命名空间。 “clr-namespace”URI 引用了无法找到的命名空间“WindowChild”。
我在这里做错了什么?
最佳答案
I've finally figured it out: instead of using
<Window.Resources>
andObjectDataProvider
all it needed was<Window.DataContext><src:TestSettings/></Window.DataContext>
.
关于从子窗口绑定(bind) wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696669/