wpf 在样式中指定列表框 ItemContainer 上的数据上下文类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15231120/
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:53:36  来源:igfitidea点击:

Specify datacontext type on listbox ItemContainer in style

c#wpfsilverlightxamlbinding

提问by Isantipov

In a ListBoxI have a ItemContainer's IsSelectedproperty bound to my ViewModel's IsSelectedproperty using <ListBox.ItemContainerStyle>syntax.

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

It works fine, but I get a Resharper warning:

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

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

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

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

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

Here is the code. I have a BarViewModelclass:

这是代码。我有一BarViewModel堂课:

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

BarViewModelis assigned to DataContext in the Control which contains the ListBox

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

and FooViewModelas follows:

并且FooViewModel,如下所示:

public bool IsSelected
{
    get
    {
        return isSelected;
    }

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

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

and XAML like this:

和 XAML 像这样:

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

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

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

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

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

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

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

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

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

回答by Isantipov

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

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

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

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

这消除了 Resharper 的警告,并且在 ViewModel 上重命名属性时也会更改绑定路径。凉爽的!

回答by N. Kudryavtsev

Specifying d:DataContext="{d:DesignInstance nmspc:Clz}"with other attributes of Styletag didn'thelp me: R# / IntelliSense really stopped highlighting properties I was binding to but the designer also showed me an error message instead of the view.

指定标签的d:DataContext="{d:DesignInstance nmspc:Clz}"其他属性对我没有帮助:R#/IntelliSense 确实停止了突出显示我绑定到的属性,但设计器还向我显示了错误消息而不是 viewStyle

The trick I found out is to specify <d:Style.DataContext>insidethe Styletag. And it appeared to be so universal that it answers another question, about using interfaces as d:DataContext.

我发现的技巧是在标签<d:Style.DataContext>指定Style。它似乎非常通用,以至于它回答了另一个问题,关于将接口用作d:DataContext.

Here is my answer to that question with a small example: https://stackoverflow.com/a/46637478/5598194

这是我用一个小例子回答这个问题:https: //stackoverflow.com/a/46637478/5598194

回答by Federico Berasategui

use d:DataContextlike this:

d:DataContext像这样使用:

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

You also need the following xmlnses added to the root element:

您还需要将以下xmlnses 添加到根元素:

     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d"

回答by eexaxa

Addition to previous answers: to get rid of error

除了以前的答案:摆脱错误

Property 'DataContext' is not attachable to elements of type 'Style'

属性“DataContext”不可附加到“Style”类型的元素

add some dummy namespace

添加一些虚拟命名空间

xmlns:ignore="designTimeAttribute"

and use it now instead of d:DataContext

并立即使用它而不是 d:DataContext

<Style TargetType="{x:Type ListBoxItem}" ignore:DataContext="{d:DesignInstance local:FooViewModel }">
...
</Style>