WPF UserControl组件网格定义问题

时间:2017-06-28 17:29:49

标签: c# .net wpf caliburn.micro

我有两个列的简单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>

让我们来看看我们有什么: enter image description here

完美!这正是我所需要的。

但是!如果我只是尝试为Width内的每个列设置动态DataGrid,则整个模板都会损坏:

<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>

结果如下: enter image description here

还有一个有趣的事情。如果您尝试开始调整窗口大小 - 那么一切都变得正常,例如: ScreenCast

有人可以帮我解决这个问题吗?

提前致谢。

4 个答案:

答案 0 :(得分:0)

你尝试过将MinWidth和Width组合在一起吗?见下面的例子:

<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>

答案 1 :(得分:0)

只需设置控制窗口的宽度和高度

<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"
             >

控制将表现得很完美

答案 2 :(得分:0)

问题出在空StackPanel上。您正在使用*表示动态DataGridTextColumn宽度。

  • (明星)占用任何剩余空间

详细讨论如下: Meaning of * (asterisk) in a WPF ColumnDefinition? (weighted proportion of available space)

只需用控件填充StackPanel或获取想法,将StackPanel替换为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 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>

然后,您可以观察到控制行为是正确的,因为第一个数据网格列没有剩余空间。

答案 3 :(得分:0)

问题与Caliburn无关。与此处描述的问题完全相同:Window SizeToContent issue when child UserControl has grid with Column of max Width

我以与此处提议相同的方式解决了我的问题:fix