【问题标题】:WPF - Set binding in Child UserControl from Parents pure XAMLWPF - 在来自父母纯 XAML 的子用户控件中设置绑定
【发布时间】:2015-12-10 13:53:43
【问题描述】:

我已经浏览了足够多的 InterWebs!我希望在这里解决这个问题。
我有两个父用户控件,ParentUc1ParentUc2。它们都包含一个ChildUc

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

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

ChildUc:

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

谢谢!

【问题讨论】:

    标签: wpf xaml binding user-controls


    【解决方案1】:

    由于您的子用户控件从与SensorRotationAngle 属性的绑定中获取值,因此您需要确保在您的ChildUc 上设置的DataContext 类具有这样的属性。

    所以,你可以像这样创建你的子控件,直接实例化视图模型并在过程中设置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));
        }
    }
    

    我刚刚在我的系统上测试了这个,它可以工作。

    【讨论】:

      【解决方案2】:

      我相信这是一个使用 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>
      

      【讨论】:

        猜你喜欢
        • 2012-04-15
        • 2011-02-09
        • 1970-01-01
        • 2021-10-01
        • 1970-01-01
        • 2011-01-18
        • 1970-01-01
        • 1970-01-01
        • 2010-12-26
        相关资源
        最近更新 更多