WPF DataTrigger - Setting ListBoxItem IsSelected

后端 未结 1 1645
心在旅途
心在旅途 2020-12-22 03:07

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


    
               


        
相关标签:
1条回答
  • 2020-12-22 03:56

    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.

    0 讨论(0)
提交回复
热议问题