从子窗口绑定(bind) wpf

标签 wpf vb.net xaml binding childwindow

我正在学习 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”。

我在这里做错了什么?

最佳答案

From the OP :

I've finally figured it out: instead of using <Window.Resources> and ObjectDataProvider all it needed was <Window.DataContext><src:TestSettings/></Window.DataContext>.

关于从子窗口绑定(bind) wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696669/

相关文章:

c# - 如何实现带图像但没有边框的按钮?

c# - 扩展wpf的功能

c# - Xaml 使用隐藏属性

VSTS : Different checksum values given for '*.g.cs' Files 的 WPF 警告

c# - xaml无法识别模型的属性(WPF MVVM)

c# - ListBox 作为 WPF 中的 Numeric UpDown 控件

vb.net - 列表字符串字符串索引?

mysql - 从数据库中检索空日期到 vb.net

vb.net - 如何检查输入的日期应该大于 vb.net 中的系统日期

.net - 在 XAML 中导入命名空间时会发生什么?