简体   繁体   English

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

[英]WPF - DataGrid column binding to attached property not working on context menu

I have ToggleButton s and a DataGrid , each row in DataGridColumn has a ColGroup AttachedProperty set to the name of the column group name. 我有ToggleButton S和DataGrid ,每行DataGridColumn具有ColGroup AttachedProperty设定为列组名称的名称。

Attached property: 附属物业:

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);
    }
}

The ToggleButton s has two jobs, on Check/UnCheck show/collapse all columns with the same group name. ToggleButton具有两个作业,在Check/UnCheck show/collapse具有相同组名的所有列。

and it has a ContextMenu which shows only the DataGridColumns with the same group name. 它具有一个ContextMenu ,仅显示具有相同组名的DataGridColumns

I've managed to bind all DataGridColumns to the ToggleButton , but couldn't find a way to Collapse the DataGridColumns with different group names. 我设法将所有DataGridColumns绑定到ToggleButton ,但是找不到用不同的组名折叠DataGridColumns的方法。

How to fill context menu with only the columns with the givin group name inside the Style Trigger? 如何在样式触发器中仅使用具有吉文组名称的列填充上下文菜单?

And how to hid all columns that has the group name when un-check toggle button? 以及如何取消选中切换按钮时隐藏所有具有组名称的列?

XAML: 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: 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" />

Using a DataTrigger should work as far as the binding to the attached property is concerned: 就绑定到附加属性而言,使用DataTrigger应该可以工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM