これを実現するには、依存プロパティを使用してみてください。
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は、このようになります
希望これは
を助け、あなたはより多くのコードを提供してください、あなたは地元のAncestorTypeがAnpotherViewであることを確認されますまたはAnotherView。それはタイプミスですか?実際にはコード内に存在します。 – lukai
「別のビュー」では、別のウィンドウを意味しますか?現在のウィンドウとどのように関連していますか、それらは同時に表示されるのですか?これはモーダルウィンドウですか? – andreask
Gief Explit型 'AncestorType = {x:タイプローカル:AnpotherView}'(つ◕_◕)つぼみ – Peter