【问题标题】:Listview item not selectable trigger on collection value databinding / datatrigger集合值数据绑定/数据触发器上的列表视图项不可选择触发器
【发布时间】:2017-07-31 17:33:13
【问题描述】:

我有一个带有文本块的 Listview 来填充列表,类内部是属性结果,如果我的软件能够解析文件,我会根据该属性设置文本块的样式,但另外我想这样做项目不可选择。

问题:

如何将 ItemcontainerStyle 属性 IsEnabled 绑定到集合的属性?

我试过了,还是不行:

<Setter Property="IsEnabled" Value="{Binding Path=ActionResult.Result ??? doesnt work }"/>

我当前的代码:

<ListView Grid.Column="0"  ItemsSource="{Binding Files}" SelectedItem="{Binding SelectedFile}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="IsEnabled" Value="{Binding Path=ActionResult.Result ??? doesnt work }"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListView.ItemTemplate>

                <DataTemplate DataType="{x:Type model:ActionResult}">

                    <TextBlock Text="{Binding Path=Name}">
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Setter Property="Foreground" Value="Black" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=Result}" Value="False">
                                        <Setter Property="Foreground" Value="LightGray" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

更新: 型号:

public class ActionResult
    {
        public string Name { get; set; }
        public bool Result { get; set; }
        public object Content { get; set; }
        public string Exceptionmessage { get; set; }
    }

Files 是 ActionResult 类型的 Observablecollection

Files = new ObservableCollection<Actionresult>

下面的帖子效果很好,但请看我下面的评论。

【问题讨论】:

  • Files 集合的类型是什么,ActionResult 属性在哪里定义?它的类型是什么?

标签: c# wpf xaml listview mvvm


【解决方案1】:

如果Result 属性属于Files 集合中的数据对象,如果您想在Result 属性返回false 时禁用ListBoxItem,这应该可以工作:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="IsEnabled" Value="{Binding Path=Result}"/>
    </Style>
</ListBox.ItemContainerStyle>

【讨论】:

  • 运行良好,经过编译和测试,但如果我输入 path=Result,则 xaml 代码窗口会声明路径的属性“无法在类型的数据上下文中解析属性 'Result' .... (myviewmodelnamespace"。ActionResult 数据对象模型位于不同的程序集中,但我提到了这个 xmlns:model="clr-namespace:XMLManager.Model;assembly=XMLManager" 并且 datacontext 设置为 viewmodellocator DataContext="{Binding Report , Source={StaticResource Locator}。有人能解释一下为什么在代码中声明吗?现在是我第二次遇到这个问题了。
  • stackoverflow.com/questions/25549826/… 我会尝试这个解决方案来解决我的问题
猜你喜欢
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 2017-07-27
  • 2019-03-14
  • 2018-01-05
  • 1970-01-01
相关资源
最近更新 更多