简体   繁体   English

WPF UserControl组件网格定义问题

[英]WPF UserControl component grid definition issue

I have simple UserControl with two columns and in first column I have DataGrid : 我有两列的简单UserControl,在第一列中有DataGrid

<UserControl x:Class="Caliburn_SimpleInjector_Sample.ViewModel.ShellView"
             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"
             MinWidth="1100" MinHeight="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".20*" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width=".70*" />
        </Grid.ColumnDefinitions>
        <DataGrid Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=Tasks, Mode=OneWay}"
                  SelectedItem="{Binding SelectedTask}"
                  AutoGenerateColumns="False" IsReadOnly="True"
                  CanUserResizeRows="False" GridLinesVisibility="None"
                  SelectionUnit="FullRow"
                  RowHeaderWidth="0"
                  FontSize="14">
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Col 1" Binding="{Binding Find}" />
                <DataGridTextColumn Header="Col 2" Binding="{Binding Action}" />
                <DataGridTextColumn Header="Col 3" Binding="{Binding Status}" />
            </DataGrid.Columns>
        </DataGrid>

        <GridSplitter Grid.Row="0" Grid.Column="1"
                      VerticalAlignment="Stretch"
                      ResizeBehavior="PreviousAndNext"
                      Width="5" Background="#FFBCBCBC" />

        <StackPanel Grid.Row="0" Grid.Column="2" Background="Red"></StackPanel>
    </Grid>
</UserControl>

Let's take a look what we have: 让我们看看我们有什么: 在此处输入图片说明

Perfect! 完善! That's exactly what I need. 那正是我所需要的。

But! 但! If I just try to set dynamic Width for each column inside of DataGrid , the whole template becomes broken: 如果我只是尝试为DataGrid内的每一列设置动态Width ,则整个模板会损坏:

<DataGrid.Columns>
    <DataGridTextColumn Width="0.2*" Header="Col 1 20%" Binding="{Binding Find}" />
    <DataGridTextColumn Width="0.2*" Header="Col 2 20%" Binding="{Binding Action}" />
    <DataGridTextColumn Width="0.6*" Header="Col 3 60%" Binding="{Binding Status}" />
</DataGrid.Columns>

And here is the result: 结果如下: 在此处输入图片说明

And also one interesting thing. 还有一件有趣的事情。 If you try to start resize the window - then all becomes normal, example: ScreenCast 如果您尝试开始调整窗口的大小-那么一切都会正常,例如: ScreenCast

Could someone assist me with this issue? 有人可以协助我解决这个问题吗?

Thanks in advance. 提前致谢。

Have you tried combination of MinWidth and Width with *? 您是否尝试过将MinWidth和Width与*结合使用? See example below: 请参见下面的示例:

<DataGrid.Columns>
<DataGridTextColumn MinWidth="0.2" Width="*" Header="Col 1 20%" Binding="{Binding Find}" />
<DataGridTextColumn MinWidth="0.2" Width="*" Header="Col 2 20%" Binding="{Binding Action}" />
<DataGridTextColumn MinWidth="0.6" Width="*" Header="Col 3 60%" Binding="{Binding Status}" /></DataGrid.Columns>

Just set the control window width and height 只需设置控制窗口的宽度和高度

<UserControl x:Class="Caliburn_SimpleInjector_Sample.ViewModel.ShellView"
             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"
             MinWidth="1100" MinHeight="400"
             Width="1100" Height="400"
             >

Control will behave perfectly 控制将表现完美

The problem lies in empty StackPanel. 问题出在空的StackPanel中。 You are using * for dynamic DataGridTextColumn width. 您正在使用*表示动态DataGridTextColumn的宽度。

  • (star) take up any available remaining space (星号)占用任何可用的剩余空间

As discussed in detail at: Meaning of * (asterisk) in a WPF ColumnDefinition? 如以下详细讨论: WPF ColumnDefinition中*(星号)的含义? (weighted proportion of available space) (可用空间的加权比例)

Just fill the StackPanel with controls or to get an idea, replace the StackPanel with the duplicate of DataGrid definition. 只需用控件填充StackPanel或想出一个主意,就可以用DataGrid定义的副本替换StackPanel。 As sample demo will be like: 如示例演示所示:

    <UserControl x:Class="Caliburn_SimpleInjector_Sample.ViewModel.ShellView"
             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"
             MinWidth="1100" MinHeight="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width=".20*" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width=".70*" />
        </Grid.ColumnDefinitions>
        <DataGrid Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=Tasks, Mode=OneWay}"
                  SelectedItem="{Binding SelectedTask}"
                  AutoGenerateColumns="False" IsReadOnly="True"
                  CanUserResizeRows="False" GridLinesVisibility="None"
                  SelectionUnit="FullRow"
                  RowHeaderWidth="0"
                  FontSize="14">
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Width="0.2*" Header="Col 1 20%" Binding="{Binding Find}" />
                <DataGridTextColumn Width="0.2*" Header="Col 2 20%" Binding="{Binding Action}" />
                <DataGridTextColumn Width="0.6*" Header="Col 3 60%" Binding="{Binding Status}" />
            </DataGrid.Columns>
        </DataGrid>

        <GridSplitter Grid.Row="0" Grid.Column="1"
                      VerticalAlignment="Stretch"
                      ResizeBehavior="PreviousAndNext"
                      Width="5" Background="#FFBCBCBC" />

        <DataGrid Grid.Row="0" Grid.Column="2" ItemsSource="{Binding Path=Tasks, Mode=OneWay}"
                  SelectedItem="{Binding SelectedTask}"
                  AutoGenerateColumns="False" IsReadOnly="True"
                  CanUserResizeRows="False" GridLinesVisibility="None"
                  SelectionUnit="FullRow"
                  RowHeaderWidth="0"
                  FontSize="14">
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Width="0.2*" Header="Col 1 20%" Binding="{Binding Find}" />
                <DataGridTextColumn Width="0.2*" Header="Col 2 20%" Binding="{Binding Action}" />
                <DataGridTextColumn Width="0.6*" Header="Col 3 60%" Binding="{Binding Status}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

You can then observe that behaviour of control is correct, because there is no space left to be consumed by first data grid columns. 然后,您可以观察到控件的行为是正确的,因为没有空间可用于第一个数据网格列。

Issue is not related to Caliburn . 问题与Caliburn无关。 Exact the same issue described here: Window SizeToContent issue when child UserControl has grid with Column of max Width . 与此处所述的问题完全相同: 当子UserControl具有最大宽度为Column的网格时,Window SizeToContent问题

I fixed my problem in the same way as proposed here: fix . 我以与此处建议相同的方式解决了问题: fix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM