【问题标题】:Filter clicks on Column Header过滤列标题上的点击
【发布时间】:2014-06-26 20:45:30
【问题描述】:

我正在使用我之前的问题this 中描述的构造,它看起来像

<ListView x:Name="listView">

    <ListView.View>
        <GridView />
    </ListView.View>

    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}"/>
    </ListView.Resources>

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseRightButtonUp">
            <i:InvokeCommandAction Command="{Binding RightClickOnItemCommand}"
                                   CommandParameter={Binding SelectedItem,
                                                     ElementName=listView} />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListView>

这就像一个魅力,除了当我右键单击列标题时它会触发。由于我只将 SelectedItem 作为命令的参数,并且当您单击标题时不会清除所选项目属性(我也不希望它清除),因此我看不到解决此问题的方法在视图模型方面。

到目前为止,我真的很喜欢这个解决方案,因为它提供了一种非常好的和干净的方式来处理这些事件,但是这个极端案例让我抓狂。

我知道我可以在 ListViewItem 样式中添加一个事件设置器,但是处理程序会反过来要求我编写代码隐藏,这是我想首先避免的,因此 System.Windows.Interactivity-Stuff .

是否有同样好的和干净的方法来防止这种情况发生(即不涉及我编写丑陋的代码隐藏黑客的方法)?

【问题讨论】:

  • 你可以通过ItemContainerStyle把EventTrigger放到ListViewItems中。
  • @HighCore 我不确定我是否理解。我无法将触发器添加到&lt;Style TargetType="{x:Type ListViewItem}"/&gt;,因为正如 Intellisense 告诉我的那样,它们只能添加到从依赖对象派生的东西中。不过,我可能会完全错了,我没有太多使用 WPF 开发的经验。
  • @HighCore 我想我明白你现在的意思了,但它需要相同的解决方法来允许我在回答中提到的样式中的触发器,不是吗?

标签: wpf xaml blend


【解决方案1】:

问题可以用here描述的方法解决。文章中开发的类本质上添加了一个附加属性来保存触发器。

    public static readonly DependencyProperty TemplateProperty =
        DependencyProperty.RegisterAttached("Template", 
        typeof(InteractivityTemplate), 
        typeof(InteractivityItems),
        new PropertyMetadata(default(InteractivityTemplate), OnTemplateChanged));

    private static void OnTemplateChanged(
        DependencyObject d, 
        DependencyPropertyChangedEventArgs e)
    {
        InteractivityTemplate dt = (InteractivityTemplate)e.NewValue;
        dt.Seal();
        InteractivityItems ih = (InteractivityItems)dt.LoadContent();
        BehaviorCollection bc = Interaction.GetBehaviors(d);
        TriggerCollection tc = Interaction.GetTriggers(d);

        foreach (Behavior behavior in ih.Behaviors)
            bc.Add(behavior);

        foreach (TriggerBase trigger in ih.Triggers)
            tc.Add(trigger);
    }

这现在允许向单个项目添加触发器:

<ListView.Resources>
   <Style TargetType="{x:Type ListViewItem}">
       <Setter Property="int:InteractivityItems.Template">
           <Setter.Value>
               <int:InteractivityTemplate>
                   <int:InteractivityItems>
                       <int:InteractivityItems.Triggers>
                           <i:EventTrigger EventName="MouseRightButtonUp">
                               <i:InvokeCommandAction 
                                  Command="{Binding RightClickCommand}" />
                           </i:EventTrigger>
                       </int:InteractivityItems.Triggers>
                   </int:InteractivityItems>
                </int:InteractivityTemplate>
           </Setter.Value>
       </Setter>
   </Style>
</ListView.Resources>

这实际上模拟了单击项目而不是单击列表并在单击列表时找到指针下方的项目的想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-17
    • 2020-03-15
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多