0

グリッドを含むメイン ウィンドウがあり、ウィンドウのロード イベント中に、ユーザー コントロールのインスタンスを動的に作成してグリッドに追加します。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 アプローチの何が問題なのかわかりますか?

4

1 に答える 1

0

バインディングの代わりにアラインメントを使ってみることはできますか?

<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" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

バインディングの問題は、パネル内の何かがそれを増加させると、 ActualHeightandが増加する可能性があることです。ActualWidthこれは特にStackPanels に当てはまります。

a を使用して arr を実行するGridと、親ActualWidthActualHeight. うまくいくこともありますが、多くの場合、パネル内の何かによってサイズが大きくなり、バインディングが台無しになります。

于 2009-12-11T04:29:39.970 に答える