指定在款式上列表框ItemContainer的datacontext类型 [英] Specify datacontext type on listbox ItemContainer in style

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

问题描述

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

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".

我如何指定指定的列表框ItemContainer的DataContext类型摆脱这种警告?

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

下面是code。我有一个 BarViewModel 类:

Here is the code. I have a BarViewModel class:

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

BarViewModel 被分配到的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是这样的:

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

更新
我试过设置 D:的DataContext 使用二传手,由HighCore建议,但可惜的是,它并没有帮助,甚至打破了构建:

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标签'DesignInstance不XML命名空间s​​chemas.microsoft.com/ex$p$pssion/blend/2008存在;.线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
Finaly,解决的办法是设置 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指出的解决方法是指定 D:的DataContext 从混合属性SDK然而,其样式元素itsself上设置的时候只有工作,而不是在属性setter:

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>

这消除ReSharper的的警告,也改变时,属性是基于视图模型renaimed绑定路径。太酷了!

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

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

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