2015-09-14 40 views
0

我已經瀏覽了InterWebs!我希望在這裏解決這個問題。
我有兩個父UserControls,ParentUc1ParentUc2。他們都包括一個ChildUcWPF - 從父母純XAML設置Child UserControl中的綁定

如果不添加除XAML代碼以外的任何代碼,我想從每個父項的ChildUc中設置SensorRotationAngle綁定的值。

ParentUc1:
集SensorRotationAngle至10
ParentUc2:
集SensorRotationAngle至20

ChildUc:

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

謝謝!

回答

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

我剛剛在我的系統上測試過它,它工作。

+0

非常感謝隊友,它的工作就像一個魅力! –

+0

不客氣:-) – Martin

0

我相信這是一個使用DependencyProperty的Value繼承權的例子。

基本上,childcontrol將直接繼承父控制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>