使用2个Fixed列和n个Variable列设置WPF DataGrid

时间:2017-06-02 20:20:29

标签: c# wpf xaml datagrid dynamic-columns

我有2个固定的列,一个复选框和一个日期列。其余列的列数可能不同,但这些列中的数据都是相同的信息(名称,复选框,文本,按钮)

我可以使用单个DataGrid通过模板执行此操作,还是必须为固定列提供1个DataGrid,为其余列提供另一个DataGrid?

另外,我如何模拟未知数量的列?

我的数据结构是垂直的多个读数,对于单个读数,我有一个IsSelected标志和DateTaken,然后我有一个List,它可以是1-20列。所有读数都将具有相同数量的Vitals。

Reading 1>    IsSelected | DateTaken | Vital | Vital | Vital...     
Reading 2>    IsSelected | DateTaken | Vital | Vital | Vital...     

   98.6 F
    ORAL

where each vital is two rows in a cell: Measurement                  
                                        Qualifiers 

这是Vitals的列定义

                <controls:DataGridTemplateColumn>
                    <controls:DataGridTemplateColumn.Header>
                        <StackPanel Orientation="Horizontal" DataContext="Vitals">
                            <TextBlock Text="{Binding Vital.Name}" HorizontalAlignment="Center" Margin="2" VerticalAlignment="Center" />
                            <Button Content="{Binding Vital.Unit}" Margin="1" Click="Unit_Click" />
                        </StackPanel>
                    </controls:DataGridTemplateColumn.Header>

                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid Width="120" Margin="3">
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal" HorizontalAlignment="Center" DataContext="{Binding}"
                                            Visibility="{Binding HideValues, Converter={StaticResource BoolToVisibilityInverter}}">
                                    <TextBlock Text="{Binding Value}" Margin="1" />
                                    <TextBlock Text="{Binding Unit}"  Margin="3,1" />
                                </StackPanel>

                                <CheckBox  Grid.Row="0" Grid.Column="1" IsChecked="{Binding HideValues}" Height="15" Width="15" ToolTip="Select to exclude value" DataContext="{Binding}"
                                           BorderThickness="1" Background="LightGray" BorderBrush="White"
                                           Visibility="{Binding HideCheckBox, Converter={StaticResource BoolToVisibilityInverter}}" Template="{StaticResource VitalCheckBox}" />

                                <StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Stretch" DataContext="{Binding}"
                                            Visibility="{Binding HideValues, Converter={StaticResource BoolToVisibilityInverter}}">
                                    <TextBlock Text="{Binding Qualifiers, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="1" DataContext="{Binding}"
                                               ToolTip="{Binding Qualifiers}" Width="101" />

                                    <Button    Name="SelectQualifier" Content="..." FontWeight="Bold" Command="{Binding SelectQualifiersCommand}" DataContext="{Binding}"
                                               ToolTip="Assign qualifiers" Visibility="{Binding HideQualBtn, Converter={StaticResource BoolToVisibilityInverter}}"
                                               Padding="2,0" Background="LightGray" HorizontalAlignment="Right" />
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>
                </controls:DataGridTemplateColumn>

我想在XAML中执行此操作,而不是在代码隐藏中执行此操作。我认为这是可行的,但我还没找到方法......

0 个答案:

没有答案