且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF DataTrigger - 设置ListBoxItem的IsSelected

更新时间:2022-10-21 09:53:03

看来,一旦IsSelected属性​​设置,无论是用户还是在code背后,二传手将不再工作。我不知道是否有周围的任何方式,但至少有将在您的具体情况下工作黑客攻击。你可以为你的ListBoxItem的IsEnabledChanged事件注册处理程序,然后检查数据的条件,并在处理程序中设置IsSelected如果它的数据呼叫。

例如:

 私人无效ListBoxItem_EnabledChanged(对象发件人,DependencyPropertyChangedEventArgs E)
{
    ListBoxItem中senderItem =(ListBoxItem的)寄件人;
    如果(YourDataCondition ==真)
    {
        senderItem.IsSelected = FALSE;
    }
}

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

下面是别人的企图做到这一点的我一直无法核实呢。

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 gets set fine, but IsSelected does not get set. How can I fix that?

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

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.


Edit: 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.


Edit 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>

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.

Example:

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

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.