WPF - 在来自Parent pure XAML的Child UserControl中设置绑定

时间:2015-09-14 14:07:03

标签: wpf xaml binding user-controls

我已经足够浏览了InterWebs!我希望在这里解决这个问题 我有两个父UserControls, ParentUc1 ParentUc2 。它们都包含 ChildUc

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

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

ChildUc:

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

谢谢!

2 个答案:

答案 0 :(得分:1)

Since your child user control gets the value from a binding to the SensorRotationAngle property you need to ensure that the DataContext class which is set on your ChildUc has such a property.

So, you could create your child control like this, directly instanciate the view model and set the value of SensorRotationAngle in the process:

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

The view model itself could like this:

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));
    }
}

I just tested this on my system, it works.

答案 1 :(得分:0)

I believe this is a case of using Value inheritance power from DependencyProperty.

Basically, the childcontrol will inherit the value from the parent control SensorRotationAngle value directly.

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>