WPF DataTrigger - 设置ListBoxItem IsSelected [英] WPF DataTrigger - Setting ListBoxItem IsSelected

查看:424
本文介绍了WPF DataTrigger - 设置ListBoxItem IsSelected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的多选列表框中的ListBoxItems上有以下数据触发器

I have the following data trigger on the ListBoxItems in my Multi-selection ListBox

<DataTrigger Value="True">
    <DataTrigger.Binding>
        <MultiBinding Converter="{StaticResource DisableWorkItemConverter}">
            <Binding ElementName="MainForm" Path="PickedWorkItemID"/>
            <Binding Path="Id"/>
        </MultiBinding>
    </DataTrigger.Binding>
    <Setter Property="IsEnabled" Value="False"/>
    <Setter Property="IsSelected" Value="False"/>
</DataTrigger>

IsEnabled设置正常,但IsSelected未设置。我如何解决这个问题?

IsEnabled gets set fine, but IsSelected does not get set. How can I fix that?

我尝试取出IsEnabled来查看是否与IsSelected冲突,但是当它不应该时,项目保持选中。

I tried taking out IsEnabled to see if it was conflicting with IsSelected, but the Item remained selected when it should not.

只是重申一下,我可以告诉绑定和转换器工作正常,因为IsEnabled正常工作。但是由于某些原因IsSelected将不会被取消设置。

Just to reiterate, I can tell the bindings and the converter are working fine, because IsEnabled works correctly. But for some reason IsSelected will not un-set.

编辑:我刚才发现我可能不想要像IsEnabled一样工作。因为当这个触发器评估为false时,该项被重新启用。我不希望以前未选择的行被选中,因为这个触发器不再是真的。

It just occurred to me that I may not want this to work like IsEnabled. Because when this trigger evaluates false, the item gets re-enabled. I would not want a previously unselected row to get selected just because this trigger is no longer true.

任何想法?基本上我不想选择任何残疾人行。

Any ideas? Basically I don't want any of the disabled rows to be selected.

编辑2:

我尝试添加一个正常的触发器,希望它可以链接数据触发器,也不起作用。

I tried adding a normal trigger hoping it would chain off the data trigger and that did not work either.

<Style.Triggers>
    <DataTrigger Value="True">
        <DataTrigger.Binding>
            <MultiBinding Converter="{StaticResource DisableWorkItemConverter}">
                <Binding ElementName="MainForm" Path="PickedWorkItemID"/>
                <Binding Path="Id"/>
            </MultiBinding>
        </DataTrigger.Binding>
        <Setter Property="IsEnabled" Value="False"/>
    </DataTrigger>
    <Trigger Property="IsEnabled" Value="False">
        <Setter Property="IsSelected" Value="False"></Setter>
    </Trigger>
</Style.Triggers>


推荐答案

似乎一旦设置了IsSelected属性无论用户还是代码背后,设定者将不再工作。我不知道是否有任何方法,但至少有一个黑客可以在你的具体情况下工作。您可以在ListBoxItem上为IsEnabledChanged事件注册一个处理程序,然后检查数据条件,并在处理程序中设置IsSelected(如果数据调用)。

It seems that once the "IsSelected" property is set, whether by user or in code behind, the setter will no longer work. I'm not sure if there is any way around that, but there is at least a hack that would work in your specific case. You could register a handler for the "IsEnabledChanged" event on your ListBoxItem and then check your data condition and set IsSelected in the handler if the data calls for it.

示例:

private void ListBoxItem_EnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    ListBoxItem senderItem = (ListBoxItem)sender;
    if (YourDataCondition == true)
    {
        senderItem.IsSelected = false;
    }
}

我能够找到的唯一其他解决方案将添加一些依赖属性到您的ListBoxItem,注册一个类似的方法到它的OnPropertyChanged事件,并更改您的DataTrigger中的属性。

The only other solution I've been able to find would be to add some dependency property to your ListBoxItem, register a similar method to its "OnPropertyChanged" event, and change that property in your DataTrigger.

这是别人尝试这样做,我还没有能够验证还有。

Here is someone else's attempt to do this that I haven't been able to verify yet.

这篇关于WPF DataTrigger - 设置ListBoxItem IsSelected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆