2017-07-04 4 views
0

ドックパネルとアライメントを使用してコントロールの高さをビューに設定しました。 このコントロールの計算サイズを別のビューの別のコントロールの入力として使用したいとします。別のビューでコントロールのプロパティにバインドする

<Button Height="{Binding Path=Button.Height, RelativeSource={RelativeSource AncestorType={x:Type local:View1}}}" /> 

を(私は最初のビューにボタンの高さをバインドしたい)。しかし、それは動作しませんメインウィンドウ

<StackPanel> 
    <local:View1 /> 
    <local:View2 /> 
</StackPanel> 

View1を

<DockPanel> 
    ... 
     <Button x:Name="myButton" /> 
    ... 
</DockPanel> 

VIEW2 ...

I可能であればバインディングを持つxaml-onlyソリューションを探しています...

+0

を助け、あなたはより多くのコードを提供してください、あなたは地元のAncestorTypeがAnpotherViewであることを確認されますまたはAnotherView。それはタイプミスですか?実際にはコード内に存在します。 – lukai

+0

「別のビュー」では、別のウィンドウを意味しますか?現在のウィンドウとどのように関連していますか、それらは同時に表示されるのですか?これはモーダルウィンドウですか? – andreask

+0

Gief Explit型 'AncestorType = {x:タイプローカル:AnpotherView}'(つ◕_◕)つぼみ – Peter

答えて

0

これを実現するには、依存プロパティを使用してみてください。

View1を::

<DockPanel> 
     <Button x:Name="myButton" Content="Button in view1" FontSize="32"/> 
    </DockPanel> 

View1を分離コードをここにあなたのケースに基づくサンプルです。私たちは、ボタンの実際の高さを得るために、私たちが作成したDependencyPropertyにその値を代入するためにロードされたイベントを処理していることに注意してください:

public static readonly DependencyProperty ButtonHeightProperty = DependencyProperty.Register(
      "ButtonHeight", typeof (double), typeof (View1), new PropertyMetadata(default(double))); 

     public double ButtonHeight 
     { 
      get { return (double) GetValue(ButtonHeightProperty); } 
      set { SetValue(ButtonHeightProperty, value); } 
     } 
     public View1() 
     { 
      InitializeComponent(); 
     } 

     private void View1_OnLoaded(object sender, RoutedEventArgs e) 
     { 
      ButtonHeight = myButton.ActualHeight; 
     } 

次にVIEW2に、我々はそのユーザーコントロール内の別の依存関係プロパティにボタンの高さをバインド:

そしてVIEW2分離コードで:

public static readonly DependencyProperty ButtonHeightProperty = DependencyProperty.Register(
    "ButtonHeight", typeof (double), typeof (View2), new PropertyMetadata(default(double))); 

public double ButtonHeight 
{ 
    get { return (double) GetValue(ButtonHeightProperty); } 
    set { SetValue(ButtonHeightProperty, value); } 
} 

public View2() 
{ 
    InitializeComponent(); 
} 
<StackPanel> 
    <local:View1 x:Name="View1"/> 
    <local:View2 ButtonHeight="{Binding ElementName=View1,Path=ButtonHeight}"/> 
</StackPanel> 

そして出力:最後にメイン・ウィンドウのXAMLは、このようになります

enter image description here

希望これは

関連する問題