グリッドを含むメイン ウィンドウがあり、ウィンドウのロード イベント中に、ユーザー コントロールのインスタンスを動的に作成してグリッドに追加します。ActualWidth
メインウィンドウのサイズが変更されたときにユーザーコントロールを適応させるために、ユーザーコントロールの幅と高さをグリッドのとにバインドしたいと思いますActualHeight
。
最初の方法は、window_loaded イベントの同じ場所で、コードでバインド オブジェクトを作成することです。
Binding widthBinding = new Binding("ActualWidth");
widthBinding.Source = panel;
BindingOperations.SetBinding(uc, WidthProperty, widthBinding);
Binding heightBinding = new Binding("ActualHeight");
heightBinding.Source = panel;
BindingOperations.SetBinding(uc, HeightProperty, heightBinding);
panel.Children.Add(uc);
期待どおりに機能しました。
2 番目の方法は、ユーザー コントロールの xaml ファイルで xaml バインディングを使用することです。
<UserControl x:Class="S2T.RAHS2.ContentAcquisition.FileViewer.WordViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded"
Width="{Binding ElementName=ContainerElement, Path=ActualWidth}"
Height="{Binding ElementName=ContainerElement, Path=ActualHeight}">
また
<UserControl x:Class="S2T.RAHS2.ContentAcquisition.FileViewer.WordViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="UserControl_Loaded" Unloaded="UserControl_Unloaded"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, Path=ActualHeight}">
しかし、これはうまくいきませんでした。
xaml アプローチの何が問題なのかわかりますか?