wpf - 依赖属性没有更新我的用户控件

标签 wpf binding dependency-properties

下面的行适用于 TextBox DP 正文 ,其中 CellNo 是派生自 INotifyPropertychanged 的​​类的属性。所以在这里,当我更改 CellNo 时,文本将被更新,当我更改 CellNo 时,文本将被更新。这将正常工作。

Text="{Binding Path = CellNo, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}"

我创建了一个只包含一个文本框的用户控件。我定义了一个 DP 名称 CellValue 如下:
public string CellValue
    {
        get { return (string)GetValue(CellValueProperty); }
        set { SetValue(CellValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for LimitValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CellValueProperty =
        DependencyProperty.Register("CellValue", typeof(string), typeof(control), new FrameworkPropertyMetadata
        {
            BindsTwoWayByDefault = true,
        });

现在,当我在任何对话框中使用此用户控件并执行与上述相同的绑定(bind)时,目标(用户控件内的 TextBox)不会更新。
 <local:control
        x:Name="control" 
        CellValue="{Binding Path = CellNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

同样在用户控件中,我已将 TextBox 的 Text 属性绑定(bind)到 CellValue DP。

内部用户控制
<TextBox                 
        Text="{Binding Path = CellValue}"
        Name="textBox2" />

我希望当 CellValue 更改时 TextBox 文本也应该更新,但是使用上述方法它仍然是空白的。

最佳答案

这段代码

<local:control x:Name="control"  
               CellValue="{Binding Path=CellNo,
                                   Mode=TwoWay,
                                   UpdateSourceTrigger=PropertyChanged}">

正在尝试绑定(bind) UserControl 的属性 CellNo。添加 RelativeSource 或 ElementName 即可。
<local:control x:Name="control"  
               CellValue="{Binding Path=CellNo,
                                   RelativeSource={RelativeSource AncestorType={x:Type Window}}, 
                                   Mode=TwoWay,
                                   UpdateSourceTrigger=PropertyChanged}">

<local:control x:Name="control"  
               CellValue="{Binding Path=CellNo,
                                   ElementName=myWindow,
                                   Mode=TwoWay,
                                   UpdateSourceTrigger=PropertyChanged}">

您可能还需要将控件的 DataContext 设置为自身
public control()
{
    InitializeComponent();
    this.DataContext = this;
    //...
}

更新

您可以下载此 here 的示例应用程序.

否则,这是我的完整示例代码。

主窗口.xaml
<Window x:Class="DependencyPropertyInsideUserControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DependencyPropertyInsideUserControl"
        Title="MainWindow" Height="350" Width="525"
        Name="myWindow">
    <Grid>
        <local:control x:Name="control"
                       CellValue="{Binding Path = CellNo, Mode=TwoWay, ElementName=myWindow, UpdateSourceTrigger=PropertyChanged}"/>
            <Button Content="Update CellNo" Height="23" HorizontalAlignment="Left" Margin="185,149,0,0" Name="button1" VerticalAlignment="Top" Width="94" Click="button1_Click" />
    </Grid>
</Window>

主窗口.xaml.cs
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        CellNo = "Hello";
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        CellNo = "Hi";
    }
    private string m_cellNo;
    public string CellNo
    {
        get
        {
            return m_cellNo;
        }
        set
        {
            m_cellNo = value;
            OnPropertyChanged("CellNo");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

控制.xaml
<UserControl x:Class="DependencyPropertyInsideUserControl.control"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBox Text="{Binding Path = CellValue}" 
                 Name="textBox2" />
    </Grid>
</UserControl>

控制.xaml.cs
public partial class control : UserControl
{
    public string CellValue
    {
        get { return (string)GetValue(CellValueProperty); }
        set { SetValue(CellValueProperty, value); }
    }
    // Using a DependencyProperty as the backing store for LimitValue.  This enables animation, styling, binding, etc...    
    public static readonly DependencyProperty CellValueProperty =
        DependencyProperty.Register("CellValue", typeof(string), typeof(control), new FrameworkPropertyMetadata
        {
            BindsTwoWayByDefault = true,
        });
    public control()
    {
        InitializeComponent();
        this.DataContext = this;
        CellValue = "Test";
    }
}

关于wpf - 依赖属性没有更新我的用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4182165/

相关文章:

wpf - 使用 MVVM 和 WPF 创建导航的最佳架构

wpf - 使用 MVVM 链接依赖属性

c# - WPF 用户控件预配置

python - Python中的 "bind to variables"和 "bind to object"有什么区别

android - jar 绑定(bind) zubhium sdk 的 Monodroid 问题

WPF:使用控件的多个实例时,自定义控件的 DependencyProperty 失败

c# - ContentControl 和 CollectionView.CurrentItem

c# - Winforms Drawing.rectangle.union 的 WPF 等价物是什么?

c# - 使用复选框在 ListBox 中加载数据

javascript - 在包装集上绑定(bind) jQuery 单击事件