C# 如何使WPF UserControl中的一个DependencyProperty更改另一个DependencyProperty
我遵循了本文中的示例:并从这里下载了示例: 我要做的是在UserControl上添加一个新属性,该属性的值是根据现有属性CellValue设置的 因此usercontrol如下所示:C# 如何使WPF UserControl中的一个DependencyProperty更改另一个DependencyProperty,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我遵循了本文中的示例:并从这里下载了示例: 我要做的是在UserControl上添加一个新属性,该属性的值是根据现有属性CellValue设置的 因此usercontrol如下所示: <UserControl x:Class="DependencyPropertyInsideUserControl.control" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmln
<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 Height="Auto" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="{Binding Path = CellValue}" Name="textBox2" />
<TextBox Grid.Row="1" Text="{Binding Path = MyNewValue}" Name="myNewTextBox" /> <-- I have added this in
</Grid>
并在CellValue处理程序中对SetValue(MyNewValueProperty,value)的第二次调用中添加:
public string CellValue
{
get
{
return (string)GetValue(CellValueProperty);
}
set
{
SetValue(CellValueProperty, value);
SetValue(MyNewValueProperty, value); <-- Update MyNewValueProperty whenever CellValueProperty gets updated
}
}
公共字符串值
{
得到
{
返回(字符串)GetValue(CellValueProperty);
}
设置
{
SetValue(CellValueProperty,value);
SetValue(MyNewValueProperty,value);您缺少的是“CLR‘包装器’”与基础的DependencyProperty之间的差异。您可以在此处阅读它们:。我将提请您注意一个特定的引用:
在代码中设置依赖属性值通常只是对CLR“包装器”公开的set实现的调用。获取属性值本质上也是对get“包装器”实现的调用。您还可以直接调用属性系统APIGetValue
和SetValue
。
当绑定需要更新依赖属性时,WPF框架在内部就是这样做的。它不调用“CLR‘包装器’”(即公共字符串CellValue
),而是直接调用SetValue
因此,如果您需要在依赖项属性发生更改时执行某些操作,则不要像处理普通属性那样将其放入集中。相反,您需要注册一个。更改时将调用该属性,并且参数包含该属性的旧值和新值
public string CellValue
{
get { return (string)GetValue(CellValueProperty); }
set { SetValue(CellValueProperty, value); }
}
public static readonly DependencyProperty CellValueProperty =
DependencyProperty.Register("CellValue", typeof(string), typeof(control),
new PropertyMetadata(default(string), CellValue_Changed));
private static void CellValue_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((control)d).MyNewValue = (string)e.NewValue;
}
您缺少的是“CLR‘wrapper’”与基础的dependencProperty
之间的差异。您可以在此处阅读它们:。我将提请您注意一个特定的引用:
在代码中设置依赖属性值通常只是对CLR“包装器”公开的set实现的调用。获取属性值本质上也是对get“包装器”实现的调用。您还可以直接调用属性系统APIGetValue
和SetValue
。
当绑定需要更新依赖属性时,WPF框架在内部就是这样做的。它不调用“CLR‘包装器’”(即公共字符串CellValue
),而是直接调用SetValue
因此,如果您需要在依赖项属性发生更改时执行某些操作,则不要像处理普通属性那样将其放入集中。相反,您需要注册一个。更改时将调用该属性,并且参数包含该属性的旧值和新值
public string CellValue
{
get { return (string)GetValue(CellValueProperty); }
set { SetValue(CellValueProperty, value); }
}
public static readonly DependencyProperty CellValueProperty =
DependencyProperty.Register("CellValue", typeof(string), typeof(control),
new PropertyMetadata(default(string), CellValue_Changed));
private static void CellValue_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((control)d).MyNewValue = (string)e.NewValue;
}