3

我正在学习 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 报告错误:名称空间“clr-namespace:WpfApplication2”中不存在名称“TestSettings”。(第 7 行,第 49 列)

我尝试将此部分更改为WindowChild.TestSettings,但随后 VS 抱怨不支持嵌套类型。

将 CLR 命名空间更改为WpfApplication2.WindowChild甚至WindowChild不起作用,VS 说:未定义的 CLR 命名空间。“clr-namespace” URI 引用了无法找到的命名空间“WindowChild”。

我在这里做错了什么?

4

1 回答 1

1

来自 OP

我终于想通了:而不是使用<Window.Resources>ObjectDataProvider需要的只是<Window.DataContext><src:TestSettings/></Window.DataContext>.

于 2013-09-17T19:00:55.697 回答