WPF DataTrigger - 设置 ListBoxItem IsSelected
我的多选 ListBox 中的 ListBoxItems 上有以下数据触发器
<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 来查看它是否与 IsSelected 冲突,但该项目在不应该被选中的情况下仍保持选中状态。
重申一下,我可以看出绑定和转换器工作正常,因为 IsEnabled 工作正常。但由于某种原因 IsSelected 不会取消设置。
编辑:我只是想到我可能不希望它像 IsEnabled 一样工作。因为当此触发器评估为 false 时,该项目将重新启用。我不希望仅仅因为此触发器不再为真而选择先前未选择的行。
有什么想法吗?基本上我不想选择任何禁用的行。
编辑2:
我尝试添加一个普通触发器,希望它能够链接数据触发器,但这也不起作用。
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎一旦设置了“IsSelected”属性,无论是由用户还是在后面的代码中,设置器将不再起作用。我不确定是否有任何方法可以解决这个问题,但至少有一个适合您的具体情况的黑客方法。您可以在 ListBoxItem 上为“IsEnabledChanged”事件注册一个处理程序,然后检查您的数据条件,并在数据需要时在处理程序中设置 IsSelected。
示例:
我能找到的唯一其他解决方案是向 ListBoxItem 添加一些依赖属性,向其“OnPropertyChanged”事件注册类似的方法,然后在 DataTrigger 中更改该属性。
这是 其他人尝试这样做,但我还无法验证。
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:
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.