Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

c# - Specify datacontext type on listbox ItemContainer in style

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

It works fine, but I get a Resharper warning:

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

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

Here is the code. I have a BarViewModel class:

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

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

and FooViewModel as follows:

public bool IsSelected
{
    get
    {
        return isSelected;
    }

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

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

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>

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}"/>

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

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>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...