WPF从子窗口绑定

3
我正在学习XAML,并且在将控件的内容绑定到子窗口的设置属性时遇到了问题。
以下是我制作的一个示例,以使其更加清晰。从主窗口调用子窗口:
```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'。 我在这里做错了什么?

尝试使用以下代码:xmlns:src="clr-namespace:WpfApplication2;assembly=WpfApplication2"。如果不起作用,请清理解决方案并重新构建所有内容。 - Dilshod
我终于想通了:不需要使用<Window.Resources>ObjectDataProvider,只需要<Window.DataContext><src:TestSettings/></Window.DataContext>就可以了。 - Kaidan Alenko
2
既然你自己找到了答案,那么你应该编写自己的答案并将其标记为已解决! - Tyler Morrow
1个回答

1

来自OP:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接