3

我有一个定义了 DataTemplate 的 WPF ListBox。在那个模板中,我有一个网格,第一列的宽度应该占据网格中所有剩余的空间。

这似乎在 ListBox 之外有效,但在内部无效。为什么会这样,我怎样才能让它表现得一样?

这是我的代码。见第 36 行和第 70 行

 <UserControl x:Class="Russound.Windows.UI.UserControls.MAX.Reports.UniversalReportsWpf"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="250" Width="900"  >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>           
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>              

                <TextBlock Grid.Row="0" Height="35" Padding="0,3,5,0" Text="Standard Reports" HorizontalAlignment="Right" FontWeight="Bold" FontSize="20" Foreground="DarkBlue"/>
                <Border Grid.Row="1">
                    <Grid>
                        <Grid.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left"/>
                                <Setter Property="VerticalAlignment" Value="Top"/>
                                <Setter Property="Height" Value="20"/>
                                <Setter Property="Padding" Value="0,3,5,0"/>
                                <Setter Property="Cursor" Value="Hand"/>
                                <Setter Property="Foreground" Value="WhiteSmoke" />
                            </Style>
                        </Grid.Resources>
                        <Grid.Background>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF808080" Offset="0"/>
                                <GradientStop Color="#FF000000" Offset="1"/>
                            </LinearGradientBrush>
                        </Grid.Background>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="300*" />
                            <ColumnDefinition Width="150" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="50" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="Report Name" FontWeight="Bold"/>
                        <TextBlock Grid.Column="1" Text="Last Run Date" FontWeight="Bold"/>
                        <TextBlock Grid.Column="2" Text="Last Ran By" FontWeight="Bold"/>
                        <TextBlock Grid.Column="3" Text="Secure" FontWeight="Bold"/>
                    </Grid>
                </Border>
            </Grid>
            <ListBox Grid.Row="1"  ItemsSource="{Binding}" 
                     ItemTemplate="{DynamicResource reportLayout}" 
                     VirtualizingStackPanel.VirtualizationMode="Recycling"
                     MouseDoubleClick="DisplaySelectedReport" Grid.RowSpan="2">
                <ListBox.SelectedItem>
                    <MouseBinding MouseAction="LeftDoubleClick"
                  Command="ApplicationCommands.Open" />
                </ListBox.SelectedItem>
                <ListBox.Resources>
                    <DataTemplate x:Key="reportLayout" DataType="AdHockReport">
                        <Grid>
                            <Grid.Resources>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="HorizontalAlignment" Value="Left"/>
                                    <Setter Property="VerticalAlignment" Value="Top"/>
                                    <Setter Property="Height" Value="25"/>
                                    <Setter Property="Padding" Value="0,3,5,0"/>
                                    <Setter Property="Cursor" Value="Hand"/>
                                </Style>                            
                            </Grid.Resources>    

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="150" />
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="50" />
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" ToolTip="Report Name" Text="{Binding Path=DisplayName}" FontWeight="Bold"/>
                            <TextBlock Grid.Column="1" ToolTip="Last Run Date" Text="{Binding Path=LastRunDate, StringFormat=MMM dd\, yyyy h:mm tt}" />
                            <TextBlock Grid.Column="2" ToolTip="Last Run By" Text="{Binding Path=LastRunBy}" />
                            <TextBlock Grid.Column="3" ToolTip="Secure" Text="{Binding Path=IsSecure}" />
                        </Grid>                    
                    </DataTemplate>
                </ListBox.Resources>
                <ListBox.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFE5E5E5" Offset="0"/>
                        <GradientStop Color="#FF888888" Offset="1"/>
                    </LinearGradientBrush>
                </ListBox.Background>           
            </ListBox>

        </Grid>
    </UserControl>
4

2 回答 2

10

这是因为包含网格的 ListBoxItem 根据内容调整大小。因此,要填充的星型列的“剩余空间”仅与列需要的一样多。

要解决此问题,请使用 ListBox.ItemContainerStyle 将 ListBoxItem.Horizo​​ntalContentAlignment 设置为 Stretch。

于 2009-02-05T20:29:35.960 回答
0

尝试编辑 ListBox 的 ControlTemplate 并删除其中的滚动查看器,让它只是 Border 内的 ItemsPresenter。

于 2009-02-05T20:27:36.823 回答