WPF - 在纯父项的子用户控件中设置绑定 XAML

WPF - Set binding in Child UserControl from Parents pure XAML

InterWebs我浏览够了!我寄希望于解决这个问题。
我有两个父用户控件,ParentUc1ParentUc2。它们都包含一个 ChildUc.

除了 XAML 代码之外不添加任何代码,我想在每个父级的 ChildUc 中设置 SensorRotationAngle 绑定的值。

ParentUc1:
将 SensorRotationAngle 设置为 10
ParentUc2:
将 SensorRotationAngle 设置为 20

ChildUc:

<Rectangle>
    <Rectangle.RenderTransform>
        <RotateTransform Angle="{Binding SensorRotationAngle}" />
    </Rectangle.RenderTransform>
</Rectangle>

谢谢!

由于您的子用户控件从 SensorRotationAngle 属性 的绑定中获取值,因此您需要确保 DataContext class 在您的 ChildUc有这样一个属性.

因此,您可以像这样创建子控件,直接实例化视图模型并在过程中设置 SensorRotationAngle 的值:

<ChildUc>
  <ChildUc.DataContext>
    <ChildUcViewModel SensorRotationAngle="30"></ChildUcViewModel>
  </ChildUc.DataContext>
</ChildUc>

视图模型本身可能是这样的:

public class ChildUcViewModel : INotifyPropertyChanged
{
    public int SensorRotationAngle
    {
        get
        {
            return _sensorRotationAngle;
        }
        set
        {
            if (_sensorRotationAngle != value)
            {
                _sensorRotationAngle = value;
                OnPropertyChanged();
            }
        }
    }
    int _sensorRotationAngle;



    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

我刚刚在我的系统上测试了这个,它有效。

我认为这是使用 DependencyProperty 的值继承权的情况。

基本上子控件会直接继承父控件的SensorRotationAngle值。

public class ParentControlGrid : Grid
{
    // Dependency Property
    public static readonly DependencyProperty SensorRotationAngleProperty =
         DependencyProperty.Register("SensorRotationAngle", typeof(int),
         typeof(ParentControlGrid), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.Inherits));

    // .NET Property wrapper
    public int SensorRotationAngle
    {
        get { return (int)GetValue(SensorRotationAngleProperty); }
        set { SetValue(SensorRotationAngleProperty, value); }
    }
}

public class ChildControlTextBox : TextBox
{
    // Dependency Property
    public static readonly DependencyProperty SensorRotationAngleProperty;

    static ChildControlTextBox()
    {
        SensorRotationAngleProperty = ParentControlGrid.SensorRotationAngleProperty.AddOwner(typeof(ChildControlTextBox),
            new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.Inherits));
    }

    // .NET Property wrapper
    public int SensorRotationAngle
    {
        get { return (int)GetValue(SensorRotationAngleProperty); }
        set { SetValue(SensorRotationAngleProperty, value); }
    }        
}

<Window x:Class="WpfTestProj.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfTestProj="clr-namespace:WpfTestProj"        
    Title="MainWindow" Height="350" Width="525">
<wpfTestProj:ParentControlGrid SensorRotationAngle="500">
    <wpfTestProj:ChildControlTextBox Text="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=SensorRotationAngle}" />
</wpfTestProj:ParentControlGrid>