Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基本WPF数据绑定问题_Wpf_Data Binding_Dependency Properties - Fatal编程技术网

基本WPF数据绑定问题

基本WPF数据绑定问题,wpf,data-binding,dependency-properties,Wpf,Data Binding,Dependency Properties,我还有一个WPF数据绑定问题。。。一个我在任何地方都找不到答案的问题,这让我感到惊讶,因为它似乎非常基本 本质上,我的代码中有一个字符串,我希望在GUI中与文本框建立双向绑定。我认为这是一个简单的问题,在代码中创建DependencyProperty,然后通过源绑定将其绑定到TextBox。问题是,我不能把一个或两个部分都做好 下面是我在代码背后的DependencyProperty定义: public static readonly DependencyProperty FilePat

我还有一个WPF数据绑定问题。。。一个我在任何地方都找不到答案的问题,这让我感到惊讶,因为它似乎非常基本

本质上,我的代码中有一个字符串,我希望在GUI中与文本框建立双向绑定。我认为这是一个简单的问题,在代码中创建DependencyProperty,然后通过源绑定将其绑定到TextBox。问题是,我不能把一个或两个部分都做好

下面是我在代码背后的DependencyProperty定义:

    public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register( "FilePath", typeof(string), typeof(Window1));
    public string FilePath
    {
        get { return (string)GetValue(FilePathProperty); }
        set { SetValue( FilePathProperty, value); }
    }
这是我的XAML:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ReportingInterface Test Application" Height="300" Width="536">

    <Menu DockPanel.Dock="Top">
        <MenuItem Name="menu_plugins" Header="File">
            <MenuItem Header="Open">
                <StackPanel Orientation="Horizontal">
                    <Label>File location:</Label>
                    <TextBox Name="text_filepath" Width="100" Text="{Binding Source=FilePath, Path=FilePath, Mode=TwoWay}"></TextBox>
                    <Button Margin="3" Width="20">...</Button>
                </StackPanel>
            </MenuItem>
        </MenuItem>
    </Menu>

文件位置:
...

我所知道的明显错误的部分是装订部分。。。我不想把人们的时间浪费在这个问题上,但老实说,我在每一次搜索中都发现了不足(但现在至少这个请求将填充后续的谷歌搜索)。:)


谢谢大家!

在XAML中定义绑定时,它将绑定到任何设置为对象(或其父对象)的DataContext的对象

这通常意味着您将窗口的DataContext设置为某个类,然后绑定将工作:

<TextBox Name="text_filepath" Width="100" Text="{Binding Path=FilePath, Mode=TwoWay}" />
这将使绑定对窗口本身起作用

或者,可以将绑定设置为针对特定源对象进行绑定。在本例中,如果您希望能够使用其他内容作为DataContext,但仍然希望绑定到窗口中定义的依赖项属性,则可以执行以下操作:

<TextBox Name="text_filepath" Width="100" Text="{Binding Path=FilePath, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"></TextBox>


这是通过告诉绑定找到类型为“Window”的第一个祖先,并将其绑定到该对象上的“FilePath”属性来实现的。

对于它的价值,我建议研究M-V-VM模式(Model、View、ViewModel)-本质上,您所做的是使用这个类作为XAML的数据上下文,你所有有趣的公开属性/命令/你拥有的东西都公开为该类(称为ViewModel)的公共成员

下面是一个很好的概述网络广播:

这是MSDN杂志的另一篇:

谢谢你,里德!我已经玩过DataContext,但仍然有点模糊,无法让它与我当前的项目一起工作。我现在就要试一试!:)是的,宝贝!谢谢,这条神奇的线this.DataContext.this很有魅力。:)MVVM绝对是我需要学习的概念中不断增长的一个。这对我来说绝对是必须的。
<TextBox Name="text_filepath" Width="100" Text="{Binding Path=FilePath, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"></TextBox>