<columndefinition width =“*”>在DataTemplate </columndefinition>中未得到遵守

时间:2009-02-05 20:17:46

标签: wpf .net-3.5 datatemplate

我有一个带有已定义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>

2 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:0)

尝试编辑ListBox的ControlTemplate并删除其中的scrollviewer,让它只是Border中的ItemsPresenter。