XAML 如何将DataGridColumn属性传递给包含项的弹出窗口,然后传递给项的单击处理程序?

2nbm6dog  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(111)

我有一个DataGrid,其中所有列都使用相同的HeaderTemplate。该HeaderTemplate有一个DropDownButton,可以打开一个DropDown。该Dropdown具有每个都有一个单击处理程序的项。我需要将列的SortMemberPath传递给项目单击处理程序。
怎么做?
当前尝试:

<Popup.Style>
                <Style TargetType="{x:Type Popup}">
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=myButton, Path=IsMouseOver}" Value="True" />
                                <Condition Binding="{Binding ElementName=myItemsControl, Path=IsMouseOver}" Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter Property="IsOpen" Value="True" />
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </Popup.Style>
            <Border BorderThickness="1" BorderBrush="Black" Background="White">
                <ItemsControl ItemsSource="{Binding ElementName=myButton, Path=DropDown.Items}" />
            </Border>
        </Popup>
        <DataTemplate x:Key="ColumnFilterHeaderTemplate">
            <Grid Width="{Binding ElementName=DeviceColumn, Path=ActualWidth}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="5"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="5"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}, Path=Content}" Grid.Column="1" HorizontalAlignment="Left"/>
                <local:DropDownButton x:Name="filterDropDownButton" Content="..." local:MouseOverHelper.Observe="True"
                                                      Width="30" Background="Transparent" Grid.Column="3" BorderBrush="Transparent" HorizontalAlignment="Right">
                    
                    <local:DropDownButton.DropDown>
                        <ItemsControl x:Name="itemsControl" Background="White">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <MenuItem Header="Option 1" Click="Option1_Click" Foreground="Black"/>
                            <MenuItem Header="Option 2" Click="Option2_Click" Foreground="Black"/>
                            <MenuItem Header="Filter" Click="FilterOption_Click" Foreground="Black" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumn}, Path=SortMemberPath}"/>
                        </ItemsControl>
                    </local:DropDownButton.DropDown>
                </local:DropDownButton>
            </Grid>
        </DataTemplate>

<DataGridTemplateColumn Header="Device" SortMemberPath="Name" Width="*" x:Name="DeviceColumn" HeaderTemplate="{StaticResource ColumnFilterHeaderTemplate}"/>

字符串
在我的代码隐藏中,CommandParameter的值为null

private void FilterOption_Click(object sender, RoutedEventArgs e)
   {
      MenuItem menuItem = sender as MenuItem;
      string sortMemberPath = menuItem.CommandParameter as string;
   }

uxhixvfz

uxhixvfz1#

DataGridColumn不是MenuItem的视觉祖先,但DataGridColumnHeader可能依赖于DropDownButton的实现:

<MenuItem Header="Filter" ... 
          CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}, 
              Path=Column.SortMemberPath}"/>

字符串

相关问题