我正在学习XAML,并且在将控件的内容绑定到子窗口的设置属性时遇到了问题。
以下是我制作的一个示例,以使其更加清晰。从主窗口调用子窗口:
```html
请注意保留HTML标记。
此时,Visual Studio 报告了一个错误:“名称‘TestSettings’在命名空间‘clr-namespace:WpfApplication2’中不存在。”(第7行,第49列) 我尝试将此部分更改为
将CLR命名空间更改为
以下是我制作的一个示例,以使其更加清晰。从主窗口调用子窗口:
```html
这里有一个快速示例:
```请注意保留HTML标记。
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'。
我在这里做错了什么?
<Window.Resources>
和ObjectDataProvider
,只需要<Window.DataContext><src:TestSettings/></Window.DataContext>
就可以了。 - Kaidan Alenko