Ash*_*shu 10 wpf binding dependency-properties
下面的行适用于TextBox DP Text,其中CellNo是从INotifyPropertychanged派生的类的属性.因此,当我更改CellNo时,文本将被更新,当我更改CellNo时,文本将被更新.这样可以正常工作.
Text="{Binding Path = CellNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Run Code Online (Sandbox Code Playgroud)
我创建了一个只包含一个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,
});
Run Code Online (Sandbox Code Playgroud)
现在,当我在任何对话框中使用此用户控件并执行与上面相同的绑定时,目标(用户控件内的TextBox)不会更新.
<local:control
x:Name="control"
CellValue="{Binding Path = CellNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
Run Code Online (Sandbox Code Playgroud)
同样在用户控件内部,我将TextBox的Text属性绑定到CellValue DP.
内部用户控制
<TextBox
Text="{Binding Path = CellValue}"
Name="textBox2" />
Run Code Online (Sandbox Code Playgroud)
我希望当CellValue更改时TextBox文本也应该更新,但是使用上面的appoach它仍然是空白的.
Fre*_*lad 14
这段代码
<local:control x:Name="control"
CellValue="{Binding Path=CellNo,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
Run Code Online (Sandbox Code Playgroud)
正试图绑定UserControl的Property 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}">
Run Code Online (Sandbox Code Playgroud)
您可能还需要将控件的DataContext设置为自身
public control()
{
InitializeComponent();
this.DataContext = this;
//...
}
Run Code Online (Sandbox Code Playgroud)
更新
您可以在此处下载此示例应用程序.
否则,这是我的完整示例代码.
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>
Run Code Online (Sandbox Code Playgroud)
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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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";
}
}
Run Code Online (Sandbox Code Playgroud)