下面的行适用于TextBox DP Text,其中CellNo是从INotifyPropertychanged派生的类的属性.所以在这里,当我更改CellNo文本将被更新,当我更改CellNo文本将被更新.这样会很好.
- Text="{Binding Path = CellNo,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
我创建了一个仅包含一个TextBox的用户控件.我已经定义了一个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,UpdateSourceTrigger=PropertyChanged}">
另外在用户控件内部,我将TextBox的Text属性绑定到CellValue DP.
内部用户控制
我想要当CellValue更改时,TextBox文本也应该被更新,但是使用上面的appoach它仍然是空白的.
这段代码
- <local:control x:Name="control"
- CellValue="{Binding Path=CellNo,UpdateSourceTrigger=PropertyChanged}">
正试图绑定UserControl的Property CellNo.添加RelativeSource或ElementName,它将工作.
- <local:control x:Name="control"
- CellValue="{Binding Path=CellNo,RelativeSource={RelativeSource AncestorType={x:Type Window}},UpdateSourceTrigger=PropertyChanged}">
- <local:control x:Name="control"
- CellValue="{Binding Path=CellNo,ElementName=myWindow,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,UpdateSourceTrigger=PropertyChanged}"/>
- <Button Content="Update CellNo" Height="23" HorizontalAlignment="Left" Margin="185,149,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,etc...
- public static readonly DependencyProperty CellValueProperty =
- DependencyProperty.Register("CellValue",});
- public control()
- {
- InitializeComponent();
- this.DataContext = this;
- CellValue = "Test";
- }
- }