如何在使用拆分器调整大小时允许wpf控件填充其网格单元格,但也具有默认宽度

时间:2013-08-06 11:05:20

标签: wpf binding grid datatemplate tabcontrol

我的情况是我在标签内有2列。第二列可以隐藏。用图片可以更好地解释问题:

没有显示第二列的初始标签应该如下所示,所以它是正确的:

sss

频道标签也应如此,这也是正确的:

enter image description here

但是当我使用网格分割器调整第二列的大小时,列表框保持固定宽度而不是拉伸。我希望它延伸:

enter image description here

现在,在调整第二个标签列表框的大小后,第一个标签有一个不需要的空白区域:

enter image description here

代码:

<TabControl x:Name="tcTabs" ItemsSource="{Binding Rooms, UpdateSourceTrigger=PropertyChanged}" Margin="5" BorderThickness="1" IsSynchronizedWithCurrentItem="True">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding Name}" />
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="22"/>
                            </Grid.RowDefinitions>

                            <ListBox ItemsSource="{Binding Messages}" DisplayMemberPath="Raw" Grid.Row="0" Grid.Column="0" BorderThickness="0" />
                            <GridSplitter ResizeDirection="Columns" VerticalAlignment="Stretch" Width="3" />
                            <ListBox ItemsSource="{Binding Users}" DisplayMemberPath="Nick" Visibility="{Binding Type, Converter={StaticResource UserListVisibilityConverter}}" Grid.Row="0" Grid.Column="1" BorderThickness="1,0,0,0" BorderBrush="#FFBBBBBB" Width="130" />
                            <TextBox Grid.Row="1" Grid.ColumnSpan="2" BorderThickness="0,1,0,0" BorderBrush="#FFBBBBBB" Height="22" />
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

首先,我会将GridSplitter放在自己的列中。其次,确保您的最终专栏也具有比例尺寸。第三,不要在最右边ListBox设置固定宽度。如果您希望它具有最小宽度,请设置MinWidth。所以你的XAML看起来像是:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="Auto"/>
    <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="22"/>
</Grid.RowDefinitions>

<ListBox ItemsSource="{Binding Messages}" DisplayMemberPath="Raw" Grid.Row="0" Grid.Column="0" BorderThickness="0" />
<GridSplitter Grid.Column="1" ResizeDirection="Columns" VerticalAlignment="Stretch" Width="3" />
<ListBox ItemsSource="{Binding Users}" DisplayMemberPath="Nick" Visibility="{Binding Type, Converter={StaticResource UserListVisibilityConverter}}" Grid.Row="0" Grid.Column="2" BorderThickness="1,0,0,0" BorderBrush="#FFBBBBBB" MinWidth="130" />
<TextBox Grid.Row="1" Grid.ColumnSpan="3" BorderThickness="0,1,0,0" BorderBrush="#FFBBBBBB" Height="22" />