【问题标题】:How to get a dynamic ListBox ItemTemplate to stretch horizontally the full width of the ListBox?如何让动态 ListBox ItemTemplate 水平拉伸 ListBox 的整个宽度?
【发布时间】:2012-11-07 20:35:28
【问题描述】:

我有一个带有自定义模板的普通列表框来显示项目。我无法管理的是在列表框的整个宽度上水平拉伸模板。

我的问题是,主窗口中的所有元素都是动态放置的,并且它们会随着窗口大小更改方法一起调整大小。我在网上搜索过,想法是把 Horizo​​ntalAligment="Stretch"。我尝试了所有可能的方法,但没有取得重大成功。

我的 xaml 代码是这样的:

<UserControl x:Class="LiveGames.Dealer.UsersList"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         Background="Transparent">
<Border CornerRadius="10" BorderBrush="White" BorderThickness="2" >
     <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="PlayerTemplate">
                <WrapPanel>
                    <Border CornerRadius="10" BorderBrush="White" BorderThickness="2" >
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Offset="0.0" x:Name="gradientOne_Name" Color="Red" />
                                <GradientStop Offset="1.0" x:Name="gradientTwo_Name" Color="DarkRed" />
                            </LinearGradientBrush>
                        </Border.Background>
                        <Grid >
                            <Grid.ColumnDefinitions x:Uid="5">
                                <ColumnDefinition  Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Name="RowH" Height="50"/>
                            </Grid.RowDefinitions>

                            <!--  <TextBlock VerticalAlignment="Center" Margin="5" Grid.Column="0" Grid.Row="1" Text="Player: " FontSize="18" />-->
                            <TextBlock VerticalAlignment="Center" Margin="5" Grid.Column="0" Text="{Binding Path=ID}" FontSize="22" FontWeight="Bold"/>
                        </Grid>
                    </Border>
                </WrapPanel>
            </DataTemplate>
        </Grid.Resources>
        <Canvas>
            <ListBox Name="userList" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" ItemsSource="{Binding}"  ItemTemplate="{StaticResource PlayerTemplate}" HorizontalContentAlignment="Stretch">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem" >
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
        </Canvas>
    </Grid>
</Border>

是否有人知道如何做到这一点,甚至可能在背后的代码中?

M

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:

    你可以试试这个:

    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Width" Value="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}"></Setter>
    </Style>
    

    【讨论】:

      【解决方案2】:

      我最近遇到了这个问题,用下面的 Xaml 解决了(适用于 ListBox.ItemContainerStyle)

      <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
          <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
          <Setter Property="HorizontalAlignment" Value="Stretch"/>
      </Style>       
      

      另外,您应该知道,通过将 ListBox 放在画布内,它不会拉伸以适应其父级。它的大小将设置为您将 ListBox 设置为 Canvases 的任何大小,它们不会对其子级执行布局。

      【讨论】:

      • 不幸的是,这对我不起作用。我试图将 listBox 从画布中取出,但这也无济于事。
      • @user1448231 如果您还没有它,我强烈建议您在 snoopwpf.codeplex.com 下载 Snoop for WPF。使用它,您可以直观地调试您的可视化树并找出哪些元素设置了哪些属性(按 Horizo​​ntalAlign 过滤)。您可能会发现绑定没有正确设置水平对齐,或者另一个元素需要水平对齐。最好的问候,
      猜你喜欢
      • 2010-10-24
      • 2017-02-27
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多