将datagrid列可见性绑定到字典值

时间:2014-02-18 17:06:06

标签: c# wpf mvvm dictionary datagrid

人!我有一个问题: 我有一个关于MVVM Light和DataGrid的应用程序:

    <DataGrid ItemsSource="{Binding Path=MyCollection, Mode=TwoWay}" AutoGenerateColumns="False" CanUserAddRows="False"
                        FontSize="14" Name="ItemGrid" SelectionUnit="FullRow" ColumnHeaderStyle="{StaticResource ResourceKey=DataGridColumnHeader}" Background="Transparent"
                   Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="4" 
                        CanUserResizeRows="False" SelectionMode="Single" BorderThickness="0">
                    <DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                            <DataGrid
                                IsReadOnly="True"
                  AutoGenerateColumns="False"
                    ItemsSource="{Binding MyItems}"
                                SelectionUnit="FullRow"
                                Visibility="{Binding Path=DataContext.RowDetailsVisibility, Mode=TwoWay,Converter={StaticResource BoolToVisibilityConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                                CanUserResizeRows="False"
                                CanUserResizeColumns="True"
                                SelectedItem="{Binding Path=DataContext.SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="ID" Visibility="Collapsed" Binding="{Binding ID}" Width="Auto" CellStyle="{StaticResource ResourceKey=DataGridCell}" />
                                    <DataGridTextColumn Header="Title" Visibility="Visible" Binding="{Binding Title}" Width="Auto" CellStyle="{StaticResource ResourceKey=DataGridCell}" />
                                </DataGrid.Columns>
                            </DataGrid>
                        </DataTemplate>
                    </DataGrid.RowDetailsTemplate>
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Item Pack ID" Width="*" CellStyle="{StaticResource ResourceKey=DataGridCell}">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=Id, Mode=TwoWay}" Style="{StaticResource ResourceKey=TextBoxStyle}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                       <DataGridTemplateColumn Header="Number" Width="*" CellStyle="{StaticResource ResourceKey=DataGridCell}">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=Number, Mode=TwoWay}" Style="{StaticResource ResourceKey=TextBoxStyle}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
     </DataGrid.Columns>
                </DataGrid.Columns>
                <DataGrid.ContextMenu>
                    <ContextMenu Name="contextMenu" Style="{StaticResource ContextStyle}">
                        </MenuItem>
                        <MenuItem Header="Packs Columns"
                                 ItemsSource="{Binding PacksCollection}"
                                  DisplayMemberPath="Key"
Command="{Binding Path=DataContext.PacksCommand, 
                       RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                                >
                        </MenuItem>
                        <MenuItem Header="Items Columns"
                       Command="{Binding Path=DataContext.ItemsCommand, 
                       RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                       ItemsSource="{Binding ItemsCollection}" DisplayMemberPath="Key">
                        </MenuItem> 

                    </ContextMenu>
                </DataGrid.ContextMenu>
            </DataGrid>
        </Grid>

所以,如果您了解,我有一个包含网格(ID,数字)的网格,每个包都有一些项目(标识,标题)。我想要一个上下文菜单,它有2个列表:Packs和Items。在每个菜单项中,我们可以选择要显示的列。 ItemsCollection和PacksCollection是Dictionary,其中String - Column Header(与数据网格中相同)和bool - visibility状态(对于Visible为true,对于Collapsed为false)。 问题:如何绑定MVVM点击菜单项,这将改变列的可见性。我尝试了一些解决方案,但它们对我不起作用。 我需要更改DataGrid列(Id,Number)和RowDetails DataGrid - Id,Title的可见性。请帮助,我不认为这太难了。 谢谢。

0 个答案:

没有答案