在样式中指定列表框 ItemContainer 上的数据上下文类型 [英] Specify datacontext type on listbox ItemContainer in style

查看:16
本文介绍了在样式中指定列表框 ItemContainer 上的数据上下文类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListBox 我有一个 ItemContainer 的 IsSelected 属性绑定到我的 ViewModel 的 IsSelected 属性使用 语法.

In a ListBox I have a ItemContainer's IsSelected property bound to my ViewModel's IsSelected property using <ListBox.ItemContainerStyle> syntax.

它工作正常,但我收到了 Resharper 警告:

It works fine, but I get a Resharper warning:

无法在FooSolution.BarViewModel"类型的数据上下文中解析属性IsSelected".

Cannot resolve property 'IsSelected' in data context of type "FooSolution.BarViewModel".

如何在 ListBox ItemContainer 上指定指定 DataContext 类型以消除此警告?

How do I specify specify DataContext type on ListBox ItemContainer to get rid of this warning?

这是代码.我有一个 BarViewModel 类:

Here is the code. I have a BarViewModel class:

public ObservableCollection<FooViewModel> FooItems { get;set; }

BarViewModel 分配给包含 ListBox 的 Control 中的 DataContext

BarViewModel is assigned to DataContext in the Control which contains the ListBox

FooViewModel 如下:

public bool IsSelected
{
    get
    {
        return isSelected;
    }

    set
    {
        if (isSelected == value)
        {
            return;
        }

        isSelected = value;
        RaisePropertyChanged(() => IsSelected);
    }
}

和 XAML 像这样:

and XAML like this:

<ListBox ItemsSource="{Binding FooItems}" SelectionMode="Multiple">        
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

更新我已经尝试按照 HighCore 的建议使用 setter 设置 d:DataContext,但不幸的是,它没有帮助,甚至破坏了构建:

Update I've tried setting d:DataContext using a setter, as suggested by HighCore, but unfortunatelly, it doesn't help and even breaks the build:

<Setter Property="d:DataContext" Value="{d:DesignInstance yourxmlns:yourItemViewModelClass}"/>

(抛出:错误 1 ​​XML 命名空间schemas.microsoft.com/expression/blend/2008"中不存在标记DesignInstance";第 31 行第 50 行.)

(Throws: Error 1 The tag 'DesignInstance' does not exist in XML namespace 'schemas.microsoft.com/expression/blend/2008';. Line 31 Position 50. )

更新 2最后,解决方案是在样式元素本身上设置 d:DataContext(见下面我的回答):

Update 2 Finaly, the solution is to set d:DataContext on the style element itself (see my answer bellow):

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}" d:DataContext="{d:DesignInstance local:FooViewModel }">
        <Setter Property="IsSelected" Value="{Binding IsSelected}" />
    </Style>

推荐答案

正如@HighCore 所指出的,解决方案是从 blend SDK 中指定 d:DataContext 属性,但是,它仅在设置时起作用在 Style 元素本身上,而不是在属性设置器中:

As pointed out by @HighCore the solution is to specify d:DataContext attribute from blend SDK, however, it worked only when set on a Style element itsself, not in the property setter:

<ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}" d:DataContext="{d:DesignInstance local:FooViewModel }">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        </Style>
</ListBox.ItemContainerStyle>

当属性在 ViewModel 上重命名时,这会删除 Resharper 的警告并更改绑定路径.酷!

This removes Resharper's warning and also changes binding Path when property is renaimed on the ViewModel. Cool!

这篇关于在样式中指定列表框 ItemContainer 上的数据上下文类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆