禁用基于绑定值 WPF 的 ListBox 项的选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14400768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:06:52  来源:igfitidea点击:

Disable selection of an ListBox item based on binding value WPF

c#wpflistboxitem

提问by user589195

Possible Duplicate:
How to disable a databound ListBox item based on a property value?

可能的重复:
如何根据属性值禁用数据绑定 ListBox 项?

I have a listboxand would like to make some of the items in the listbox non-selectable based on the a field in the binding.

我有一个listbox并且希望根据绑定中的 a 字段使列表框中的某些项目不可选择。

Ive tried setting the listboxitem.IsEnabledto false but this doesnt have teh desired effect.

我试过将 设置listboxitem.IsEnabled为 false 但这并没有达到预期的效果。

Please could someone advise if this is possible.

如果这可能的话,请有人建议。

Heres my datatemplate:

这是我的数据模板:

<DataTemplate x:Key="GridTemplate">
      <StackPanel Orientation="Horizontal">
           <Border Padding="{Binding EditorRow.RightBondIndent}" Width="50">
                  <Image x:Name="rightImg" Source="mat.png" Stretch="Fill"  />
           </Border>
      </StackPanel>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding EditorRow.MaterialType}" Value="Blank">
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>

采纳答案by Srinivas

If you want to disable a single item inside the listbox you can try this. You can do it based on your binding property. Hope it helps.

如果你想禁用列表框中的单个项目,你可以试试这个。您可以根据您的绑定属性进行操作。希望能帮助到你。