WPF - DataGrid列绑定到附加属性不在上下文菜单上工作

时间:2017-07-10 11:37:59

标签: wpf xaml datagrid contextmenu attached-properties

我有ToggleButtonDataGridDataGridColumn中的每一行都有ColGroup AttachedProperty设置为列组名称的名称。< / p>

附属物:

public class DataGridColumnsGroupProperty {
    public static readonly DependencyProperty ColGroupProperty =
        DependencyProperty.RegisterAttached("ColGroup", typeof(object), typeof(DataGridColumnsGroupProperty), new FrameworkPropertyMetadata(null));

    public static void SetColGroup(DependencyObject element, string value) {
        element.SetValue(ColGroupProperty, value);
    }

    public static string GetColGroup(DependencyObject element) {
        return (string)element.GetValue(ColGroupProperty);
    }
}

ToggleButtonCheck/UnCheck show/collapse所有具有相同组名的列上都有两个作业。

它有一个ContextMenu,只显示具有相同组名的DataGridColumns

我设法将所有DataGridColumns绑定到ToggleButton,但无法找到使用不同组名折叠DataGridColumns的方法。

如何仅使用样式触发器中的givin组名称填充上下文菜单?

如何在取消选中切换按钮时隐藏具有组名的所有列?

XAML:

<ToggleButton.ContextMenu>
    <ContextMenu x:Name="ContextMenu" ItemsSource="{Binding Columns, ElementName=ElementDataGrid, Converter={StaticResource TestConverter}}">
        <ContextMenu.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="HeaderTemplate" Value="{Binding HeaderTemplate}"/>
                <Setter Property="Header" Value="{Binding Header}"/>
                <Setter Property="StaysOpenOnClick" Value="True" />
                <Setter Property="AutomationProperties.Name" Value="{Binding Header}"/>
                <Setter Property="IsCheckable" Value="True" />
                <Setter Property="IsChecked" Value="{Binding Visibility, Mode=TwoWay, Converter={StaticResource VisibilityToBooleanConverter}}" />
                <Style.Triggers>
                    <Trigger Property="attachedProperties:DataGridColumnsGroupProperty.ColGroup" Value="FirstGroup">
                        <Setter Property="Visibility" Value="Collapsed" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ContextMenu.ItemContainerStyle>
    </ContextMenu>
</ToggleButton.ContextMenu>

DataGridColumns:

<DataGridTextColumn x:Name="StoryCol" attachedProperties:DataGridColumnsGroupProperty.ColGroup="FirstGroup" Header="{x:Static p:Resources.Story}" IsReadOnly="True" Binding="{Binding Story}" Visibility="Visible" />
<DataGridTextColumn x:Name="CadIdCol" attachedProperties:DataGridColumnsGroupProperty.ColGroup="SecondGroup" Header="{x:Static p:Resources.CadId}" IsReadOnly="False" Binding="{Binding CadId}" Visibility="Visible" />

1 个答案:

答案 0 :(得分:1)

使用DataTrigger应该对绑定到附加属性有用:

<DataTrigger Binding="{Binding Path=(attachedProperties:DataGridColumnsGroupProperty.ColGroup)}" Value="FirstGroup">
    <Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>