【发布时间】:2013-03-19 18:23:40
【问题描述】:
我正在学习 XAML,但在将控件内容绑定到子窗口中的设置属性时遇到问题。
这是一个简单的例子,我做了一个更清楚的例子。从主窗口调用孩子:
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
这是孩子:
<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 正在报告错误:名称“TestSettings”在命名空间“clr-namespace:WpfApplication2”中不存在。(第 7 行,第 49 列)
我尝试将这部分更改为WindowChild.TestSettings,但随后 VS 抱怨不支持嵌套类型。
将 CLR 命名空间更改为 WpfApplication2.WindowChild 甚至 WindowChild 都不起作用,VS 说:未定义的 CLR 命名空间。 'clr-namespace' URI 引用了一个找不到的命名空间 'WindowChild'。
我在这里做错了什么?
【问题讨论】:
-
试试这个:xmlns:src="clr-namespace:WpfApplication2;assembly=WpfApplication2"。如果它不能帮助清理解决方案并重建一切。
-
我终于想通了:不用
<Window.Resources>和ObjectDataProvider而是只需要<Window.DataContext><src:TestSettings/></Window.DataContext>。 -
既然你自己想通了,你应该自己写答案,并标记为已回答!
标签: wpf vb.net xaml binding childwindow