[英]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. 本示例按预期方式工作:选定的项目中始终写入
True
, False
选定的项目中写入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.