0

アプリケーションに新しいウィンドウを追加しています。このアプリケーションには、ビュー モデル オブジェクトの にプロパティがバインドされている がListBox含まれています。ビュー モデル オブジェクトのレンダリングに使用されるデータ テンプレートは次のとおりです。ItemsSourceObservableCollection

<DataTemplate x:Key="DomainTemplate" DataType="DomainViewModel">
    <Border BorderBrush="{Binding Converter={StaticResource BrushConverter}, Path=IsSelected}"
            BorderThickness="2"
            Margin="5"
            Name="SelectedBorder">
        <Button Click="SelectDomain_Click"
                Content="{Binding Path=Name}"
                FontSize="16"
                FontWeight="Bold"
                Height="60"
                IsEnabled="{Binding Path=CurrentSiteIsValid, RelativeSource={RelativeSource AncestorType={x:Type c:DomainPicker}}}"
                Margin="5" />
    </Border>
</DataTemplate>

HorizontalContentAlignment="Stretch"の設定を使用して、ListBoxすべてをButtonsの幅に合わせていListBoxます。また、ビュー モデル オブジェクトはデータベースから読み取られ、Name プロパティには最大 80 文字の任意の文字列を含めることができます。

問題は、ウィンドウ上に直接ある場合のキャプションが最も長い場合Buttonsの幅と同じ幅にしたいということです。Button次に、ListBoxはそれ自体を含むようにサイズを変更しButton、最後にウィンドウは に合わせてサイズ変更する必要がありますListBox

どうすればこれを機能させることができますか?

4

1 に答える 1

2

コントロールを同じ長さにするには、プロパティを に設定して をにButton追加できます。プロパティ セットを使用して 1 つの列を定義します。GridGrid.IsSharedSizeScopetrueDataTemplateSharedSizeGroup

<DataTemplate x:Key="DomainTemplate" DataType="DomainViewModel">
    <Grid Grid.IsSharedSizeScope="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" SharedSizeGroup="Button" />
        </Grid.ColumnDefinitions>
        <Border BorderBrush="{Binding Converter={StaticResource BrushConverter}, 
Path=IsSelected}" BorderThickness="2" Margin="5" Name="SelectedBorder">
            <Button Click="SelectDomain_Click" Content="{Binding Path=Name}" 
FontSize="16" FontWeight="Bold" Height="60" IsEnabled="{Binding CurrentSiteIsValid, 
RelativeSource={RelativeSource AncestorType={x:Type c:DomainPicker}}}" Margin="5" />
        </Border>
    </Grid>
</DataTemplate>

Buttonコントロールが への入力を停止するには、宣言をListBox削除します。HorizontalAlignment="Stretch"

サイズをWindowコンテンツに合わせるには、そのSizeToContentプロパティをに設定し、その宣言からすべての プロパティとプロパティWidthAndHeightを削除します。WidthHeight

それがどうなるか教えてください。

于 2013-08-22T13:42:18.113 に答える