WPF中将Control2的DP绑定到Control1的DP

Binding DP of Control2 to DP of Control1 in WPF

我有两个简单的用户控件。两者都有一个名为 Value 的依赖项 属性。现在我在我的 MainWindow 中使用控件并尝试将一个控件的值绑定到另一个控件的值!不幸的是,它只在第一次工作并且在我更改 control1 中的值后不会更新。

控件 1:

<UserControl x:Class="ControlBining.Control1"
             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="450" d:DesignWidth="800">
    <Grid>
        <TextBox Text="{Binding Value, RelativeSource={RelativeSource AncestorType=UserControl}}" Width="100"/>
    </Grid>
</UserControl>

  public static readonly DependencyProperty ValueProperty =
     DependencyProperty.Register(
        "Value", typeof(string),
        typeof(Control1)
     );

  public string Value
  {
     get => (string)GetValue(ValueProperty);
     set => SetValue(ValueProperty, value);
  }

控件 2:

<UserControl x:Class="ControlBining.Control2"
             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" 
             xmlns:local="clr-namespace:ControlBining"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock Text="{Binding Value, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    </Grid>
</UserControl>

  public static readonly DependencyProperty ValueProperty =
     DependencyProperty.Register(
        "Value", typeof(string),
        typeof(Control2)
     );

  public string Value
  {
     get => (string)GetValue(ValueProperty);
     set => SetValue(ValueProperty, value);
  }

主窗口:

<Window x:Class="ControlBining.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ControlBining"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <local:Control1 Width="100" x:Name="Control1" Value="10"/>
        <local:Control2 Width="100" Value="{Binding ElementName=Control1, Path=Value}"/>
    </StackPanel>
</Window>

UserControl 不应设置自己的 DataContext,因为这样做会破坏其依赖属性的任何基于 DataContext 的绑定。相反,在其 XAML:

中使用 RelativeSource Binding
<UserControl ...>
    <Grid>
        <TextBox Text="{Binding Value,
                        RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    </Grid>
</UserControl>

如果您想在输入文本框时更新 Value 属性,还可以设置:

<TextBox Text="{Binding Value,
                RelativeSource={RelativeSource AncestorType=UserControl},
                UpdateSourceTrigger=PropertyChanged}"/>

当您还想在 Control2 中键入时,将值绑定双向:

<local:Control2 Value="{Binding ElementName=Control1, Path=Value, Mode=TwoWay}"/>

或使值 属性 默认绑定 TwoWay:

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register(
        nameof(Value), typeof(string), typeof(Control2),
        new FrameworkPropertyMetadata(
            null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));