【问题标题】:Dependency Property is not updating my Usercontrol依赖属性没有更新我的用户控件
【发布时间】:2010-11-15 07:10:30
【问题描述】:

下面的行适用于 TextBox DP Text,其中 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,
        });

现在,当我在任何对话框中使用此用户控件并执行与上述相同的绑定时,目标(用户控件内的 TextBox)不会更新。

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

在用户控件中,我已经将 TextBox 的 Text 属性绑定到 CellValue DP。

内部用户控制

<TextBox                 
        Text="{Binding Path = CellValue}"
        Name="textBox2" />

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

【问题讨论】:

  • 您在输出窗口中看到任何数据绑定错误吗? Usercontrol 的 DataCcontext 属性是否设置为使 TextBox 绑定有效?
  • 是的,用户控件的数据上下文和父控件的一样。同样在调试时,我验证了用户控件内部的依赖属性( CellValue )已更新,但它不会更新 TexBox 的 Text 属性。我是否必须实现属性更改回调?

标签: wpf binding dependency-properties


【解决方案1】:

这段代码

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

正在尝试绑定 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 的示例应用程序。

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

MainWindow.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>

Mainwindow.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));
        }
    }
}

control.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>

control.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";
    }
}

【讨论】:

  • 我必须使用“Path=DataContext.CellNo”在窗口中进行绑定才能为我工作。
  • 是否可以在设计时在 UserControl 中看到 DefaultValue??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 2018-05-20
相关资源
最近更新 更多