Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使WPF UserControl中的一个DependencyProperty更改另一个DependencyProperty_C#_Wpf_Xaml_User Controls - Fatal编程技术网

C# 如何使WPF UserControl中的一个DependencyProperty更改另一个DependencyProperty

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上添加一个新属性,该属性的值是根据现有属性CellValue设置的

因此usercontrol如下所示:

<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“包装器”实现的调用。您还可以直接调用属性系统API
GetValue
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“包装器”实现的调用。您还可以直接调用属性系统API
GetValue
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;
}