简体   繁体   English

Visual Studio无法识别WPF中数据绑定的正确类型

[英]Visual studio does not recognize the correct type for data binding in WPF

Here is a small example that i created to illustrate my problem. 这是我创建的一个小例子来说明我的问题。

public class DataItem
{
    public DataItem() {}
    public DataItem(bool isSelected)
    {
        IsSelected = isSelected;
    }

    public bool IsSelected { get; set; }
}


public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        Items = new ObservableCollection<DataItem> {new DataItem(true), new DataItem()};
    }

    public ObservableCollection<DataItem> Items { get; set; }
}

The XAML is: XAML是:

<Window x:Class="RoomDesigner.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ignore="http://www.ignore.com"
        xmlns:viewModel="clr-namespace:RoomDesigner.ViewModel"
        mc:Ignorable="d ignore"
        Height="350"
        Width="525"
        d:DataContext="{d:DesignInstance viewModel:MainViewModel}">

    <Grid x:Name="LayoutRoot">
        <ListBox HorizontalAlignment="Left" Height="299" Margin="230,10,0,0" VerticalAlignment="Top" Width="100" 
                 SelectionMode="Multiple"
                 ItemsSource="{Binding Items}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="IsSelected" Value="{Binding IsSelected}"/>  <!--This line-->
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding IsSelected}"></TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</Window>

This example works as expected: a selected item always has True written in it, and False is written in an unselected item. 本示例按预期方式工作:选定的项目中始终写入TrueFalse选定的项目中写入False

However, Visual Studio (or Resharper) underlines the word IsSelected on the marked line and the suggestion says Cannot resolve property 'IsSelected' in data context of type 'RoomDesigner.ViewModel.MainViewModel' . 但是,Visual Studio(或Resharper)在标记的行上给单词IsSelected下划线,并且建议说Cannot resolve property 'IsSelected' in data context of type 'RoomDesigner.ViewModel.MainViewModel' It wants to bind to a MainViewModel and not to DataItem somewhy. 它想绑定到MainViewModel而不是绑定到DataItem

I use Visual Studio 13 SP3 and Resharper 8.1. 我使用Visual Studio 13 SP3和Resharper 8.1。

I'd like to know where this strange behavior comes from and if there is a way to fix it, because it is kind of annoying. 我想知道这种奇怪行为的来历以及是否有解决方法,因为这很烦人。

You should set the d:DataContext on the Style element. 您应该在Style元素上设置d:DataContext。 It is not inferenced automatically for ItemContainerStyle property. 对于ItemContainerStyle属性,不会自动推断出它。

<Style TargetType="{x:Type ListBoxItem}" d:DataContext="{d:DesignInstance viewModel:DataItem}">
    <Setter Property="IsSelected" Value="{Binding IsSelected}"/>  <!--This line-->
</Style>

See a similar question: Specify datacontext type on listbox ItemContainer in style . 看到一个类似的问题: 在style的列表框ItemContainer上指定datacontext类型
Another similar question: How does this setter end up working? 另一个类似的问题: 这个二传手将如何工作? (ListView MultiSelect) . (ListView MultiSelect)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio无法识别MvxFragment的BindingInflate函数 - Visual Studio does not recognize BindingInflate function of MvxFragment Visual Studio无法识别System.Linq - Visual Studio Does not recognize System.Linq Visual Studio 无法识别新添加的 class - Visual studio does not recognize newly added class WPF绑定,C#泛型和可为空的类型导致Visual Studio Designer中出现未处理的异常 - WPF binding, C# Generics and nullable type result in Unhandled Exception in Visual Studio Designer WPF绑定正确,但是窗口不更新 - WPF Binding is correct, but window does not update WPF 是否允许将数据网格转换为可视化类型? - Does WPF allow casting a data grid to type visual? WPF MVVM-OneWay数据绑定的正确方法 - WPF MVVM - Correct way of OneWay data binding 在C#/ WPF应用程序中使用MVVM Light时,是否确定数据绑定的正确属性返回类型? - Determining out the correct property return type for a data binding when using MVVM Light in a C#/WPF app? 绑定类型&#39;cosmosDBTrigger&#39;未注册+ Visual Studio - The binding type(s) 'cosmosDBTrigger' are not registered + Visual Studio 绑定类型不匹配时WPF异常 - WPF Exception when binding type does not match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM