在wpf中保存contextmenu的用户首选项

时间:2016-01-03 10:35:00

标签: wpf xaml contextmenu preference

我有datagrid列和contextmenu用于列的可见性。用户可以通过勾选或取消上下文菜单来更改datagrid列的可见性。是否有任何方法可以保存datagrid'columns的可见性状态由用户选择contextmenu sothat当用户下次打开应用程序时,用户获得与之前相同的可见性设置。

xaml代码:

<usercontrol.resources> 
<ContextMenu x:Key="ColumnChooserMenu" x:Shared="False"
             DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}" 
             ItemsSource="{Binding Columns, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
            <ContextMenu.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Header" Value="{Binding Header}"/>
                    <Setter Property="AutomationProperties.Name" Value="{Binding Header}"/>
                    <Setter Property="IsCheckable" Value="True" />
                    <Setter Property="IsChecked" Value="{Binding Visibility, Mode=TwoWay, Converter={StaticResource VisibilityToBoolConv}}" />
                </Style>
            </ContextMenu.ItemContainerStyle>
        </ContextMenu>

    `<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="ContextMenu" Value="{StaticResource ColumnChooserMenu}" />
    </Style>`

<DataGrid Name="DGrid"  ItemsSource="{Binding List, Mode=TwoWay}"                       ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"

         >
                <DataGrid.Columns >
                    <DataGridTextColumn Header="SN" Width="Auto"  Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={local:RowToIndexConverter}}" />
                    <DataGridTextColumn  Header="Item code" Width="Auto" Binding="{Binding MENUCODE}"  />
                    <DataGridTextColumn  Header="Item Description" Width="*" Binding="{Binding ITEMDESC}"/>
                    <DataGridTextColumn  Header="Warehouse"  Width="120" Binding="{Binding WAREHOUSE}" />
                    <DataGridTextColumn  Header="Unit"  Width="60" Binding="{Binding UNIT}"/>
                    <DataGridTextColumn  Header="Rate"  Width="80" Binding="{Binding RATE,StringFormat=#0.00}" />
                    <DataGridTextColumn  Header="Quantity"  Width="60" Binding="{Binding Quantity,StringFormat=#0.00}" />
                    <DataGridTextColumn  Header="Gross Amount"  Width="110" Binding="{Binding AMOUNT,StringFormat=#0.00}" />
                    <DataGridTextColumn  Header="Discount"  Width="90" Binding="{Binding INDDISCOUNT,StringFormat=#0.00}" />
                    <!--temp used of generic for total-->
                   <DataGridTextColumn  Header="Total"  Width="110"  Binding="{Binding GENERIC,StringFormat=#0.00}"/>
                   <DataGridTextColumn  Header="Net Amount"  Width="120" Binding="{Binding NETAMOUNT}" />

                </DataGrid.Columns>
            </DataGrid>

0 个答案:

没有答案