出现VerticalScrollBar时调整边框大小

Flo*_*Flo 6 c# wpf resize border datatemplate

让我告诉你我的XAML代码的一部分:

<ListBox Grid.Row="1"  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         ScrollViewer.IsDeferredScrollingEnabled="True"
         HorizontalAlignment="Stretch" ItemsSource="{Binding}"  Margin="1,1,0,0"
         Name="listBox_Faits"  Width="290"  VerticalAlignment="Stretch"
         SelectionChanged="listBox_Faits_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border  BorderBrush="SlateGray" BorderThickness="0.5" Margin="1,2,1,1"
                     Width="{Binding ElementName=listBox_Faits, Path=Width}">
Run Code Online (Sandbox Code Playgroud)

如果创建了太多边框(它与ObservableCollection链接),则会出现垂直滚动条,并且我的边框不会自行调整大小.(我希望看到完整的边框,我不希望它在最后被切割)

如果有人有想法,谢谢!如果您需要更多信息,请随时询问!

RGDS,

弗洛

Fre*_*lad 3

您可以通过添加此内容来使 ListBoxItem 拉伸,然后可以删除 Border 的 Width 绑定

<ListBox ...>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <!-- ... -->
Run Code Online (Sandbox Code Playgroud)