views:

816

answers:

1

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>
+1  A: 

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.

Ben Collier
That link did the trick for me. Thanks for your help!
Vaccano