ListBox WPF / MaxWidth List Width

I have a database beatbox with a DataTemplate setup. The DataTemplate contains a Grid control with two column widths Auto and *. I would like this column to always populate the ListBoxItem and never be projected behind the LisBox control to make the horizontal scrollbar visible.

I can bind MaxWidth to the DataTemplate grid using:

MaxWidth="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=ActualWidth}"

Is this the best way to achieve this?

I also have an ItemContainerStyle setting for the following. Triggers have been removed to make code smaller and easier to read.

<Style x:Key="ListBoxItemContainer" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Padding" Value="3"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Border" BorderBrush="{x:Null}" CornerRadius="4" BorderThickness="1" Margin="1">
                    <Border x:Name="InnerBorder" BorderBrush="{x:Null}" CornerRadius="4" BorderThickness="1">
                        <ContentPresenter x:Name="ContentPresenter" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
+3
source share
1 answer

ActualWidth ActualHeight, . 90% , , ( , ).

, , ListBox :

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" />

, ListBoxItem, * .

+9

Source: https://habr.com/ru/post/1711529/


All Articles