wpf – 依赖属性不更新我的Usercontrol

前端之家收集整理的这篇文章主要介绍了wpf – 依赖属性不更新我的Usercontrol前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面的行适用于TextBox DP Text,其中CellNo是从INotifyPropertychanged派生的类的属性.所以在这里,当我更改CellNo文本将被更新,当我更改CellNo文本将被更新.这样会很好.
  1. Text="{Binding Path = CellNo,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"

我创建了一个仅包含一个TextBox用户控件.我已经定义了一个DP名称CellValue如下:

  1. public string CellValue
  2. {
  3. get { return (string)GetValue(CellValueProperty); }
  4. set { SetValue(CellValueProperty,value); }
  5. }
  6.  
  7. // Using a DependencyProperty as the backing store for LimitValue. This enables animation,styling,binding,etc...
  8. public static readonly DependencyProperty CellValueProperty =
  9. DependencyProperty.Register("CellValue",typeof(string),typeof(control),new FrameworkPropertyMetadata
  10. {
  11. BindsTwoWayByDefault = true,});

现在当我在任何一个对话框中使用这个用户控件,并且执行与上述相同的绑定,目标(用户控件中的TextBox)不会被更新.

  1. <local:control
  2. x:Name="control"
  3. CellValue="{Binding Path = CellNo,UpdateSourceTrigger=PropertyChanged}">

另外在用户控件内部,我将TextBox的Text属性绑定到CellValue DP.

内部用户控制

  1. <TextBox
  2. Text="{Binding Path = CellValue}"
  3. Name="textBox2" />

我想要当CellValue更改时,TextBox文本也应该被更新,但是使用上面的appoach它仍然是空白的.

这段代码
  1. <local:control x:Name="control"
  2. CellValue="{Binding Path=CellNo,UpdateSourceTrigger=PropertyChanged}">

正试图绑定UserControl的Property CellNo.添加RelativeSource或ElementName,它将工作.

  1. <local:control x:Name="control"
  2. CellValue="{Binding Path=CellNo,RelativeSource={RelativeSource AncestorType={x:Type Window}},UpdateSourceTrigger=PropertyChanged}">
  3.  
  4. <local:control x:Name="control"
  5. CellValue="{Binding Path=CellNo,ElementName=myWindow,UpdateSourceTrigger=PropertyChanged}">

您可能还需要将控件的DataContext设置为自身

  1. public control()
  2. {
  3. InitializeComponent();
  4. this.DataContext = this;
  5. //...
  6. }

更新

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

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

MainWindow.xaml

  1. <Window x:Class="DependencyPropertyInsideUserControl.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:DependencyPropertyInsideUserControl"
  5. Title="MainWindow" Height="350" Width="525"
  6. Name="myWindow">
  7. <Grid>
  8. <local:control x:Name="control"
  9. CellValue="{Binding Path = CellNo,UpdateSourceTrigger=PropertyChanged}"/>
  10. <Button Content="Update CellNo" Height="23" HorizontalAlignment="Left" Margin="185,149,0" Name="button1" VerticalAlignment="Top" Width="94" Click="button1_Click" />
  11. </Grid>
  12. </Window>

Mainwindow.xaml.cs

  1. public partial class MainWindow : Window,INotifyPropertyChanged
  2. {
  3. public MainWindow()
  4. {
  5. InitializeComponent();
  6. this.DataContext = this;
  7. CellNo = "Hello";
  8. }
  9. private void button1_Click(object sender,RoutedEventArgs e)
  10. {
  11. CellNo = "Hi";
  12. }
  13. private string m_cellNo;
  14. public string CellNo
  15. {
  16. get
  17. {
  18. return m_cellNo;
  19. }
  20. set
  21. {
  22. m_cellNo = value;
  23. OnPropertyChanged("CellNo");
  24. }
  25. }
  26. public event PropertyChangedEventHandler PropertyChanged;
  27. private void OnPropertyChanged(string propertyName)
  28. {
  29. if (PropertyChanged != null)
  30. {
  31. PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
  32. }
  33. }
  34. }

control.xaml

  1. <UserControl x:Class="DependencyPropertyInsideUserControl.control"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="300" d:DesignWidth="300">
  8. <Grid>
  9. <TextBox Text="{Binding Path = CellValue}"
  10. Name="textBox2" />
  11. </Grid>
  12. </UserControl>

control.xaml.cs

  1. public partial class control : UserControl
  2. {
  3. public string CellValue
  4. {
  5. get { return (string)GetValue(CellValueProperty); }
  6. set { SetValue(CellValueProperty,value); }
  7. }
  8. // Using a DependencyProperty as the backing store for LimitValue. This enables animation,etc...
  9. public static readonly DependencyProperty CellValueProperty =
  10. DependencyProperty.Register("CellValue",});
  11. public control()
  12. {
  13. InitializeComponent();
  14. this.DataContext = this;
  15. CellValue = "Test";
  16. }
  17. }

猜你在找的设计模式相关文章