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

查看:21
本文介绍了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 冲突,但该 Item 在不应该被选中时保持选中状态.

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"属性被设置,无论是由用户还是在后面的代码中,设置器将不再起作用.我不确定是否有任何解决方法,但至少有一个 hack 可以在您的特定情况下使用.您可以为 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天全站免登陆