WPF DataTrigger - 设置 ListBoxItem IsSelected

发布于 2024-08-15 02:55:23 字数 1583 浏览 4 评论 0原文

我的多选 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-08-22 02:55:23

似乎一旦设置了“IsSelected”属性,无论是由用户还是在后面的代码中,设置器将不再起作用。我不确定是否有任何方法可以解决这个问题,但至少有一个适合您的具体情况的黑客方法。您可以在 ListBoxItem 上为“IsEnabledChanged”事件注册一个处理程序,然后检查您的数据条件,并在数据需要时在处理程序中设置 IsSelected。

示例:

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

我能找到的唯一其他解决方案是向 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文