C# XamlObjectWriterException:集合属性';System.Windows.Data.CollectionViewSource'';设计来源';是空的
在VS2013中,如果使用collectionView并设置listbox的Itemsource BindDisplayingList,则不会出现xaml错误。xaml可以正确地进行分析。 然而,在VS2015中,如果我们使用在VS2013中运行良好的相同项目,则会出现xaml错误,即DesignSource为null。 我只是搜索了一下错误,但找不到合适的答案。 xaml代码如下所示:C# XamlObjectWriterException:集合属性';System.Windows.Data.CollectionViewSource'';设计来源';是空的,c#,xaml,C#,Xaml,在VS2013中,如果使用collectionView并设置listbox的Itemsource BindDisplayingList,则不会出现xaml错误。xaml可以正确地进行分析。 然而,在VS2015中,如果我们使用在VS2013中运行良好的相同项目,则会出现xaml错误,即DesignSource为null。 我只是搜索了一下错误,但找不到合适的答案。 xaml代码如下所示: <Window x:Class="WpfApplication1.MainWindow"
<Window x:Class="WpfApplication1.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:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<CollectionViewSource x:Key="brandViewModelViewSource" d:DesignSource="{d:DesignInstance {x:Type local:BrandViewModel}, CreateList=True}"/>
</ResourceDictionary>
</Window.Resources>
<Grid DataContext="{StaticResource brandViewModelViewSource}" x:Name="MainContainer">
<ListBox Margin="2,120,75,0" ScrollViewer.VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Width="950" Background="White" x:Name="brandDataGrid" ItemsSource="{Binding BrandDisplayList}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Name="border" BorderBrush="LightGray" BorderThickness="1" CornerRadius="7"
Padding="0" Margin="5,15,5,15">
<StackPanel Orientation="Vertical" Width="162" >
<Grid Height="42" Width="161">
<Label FontWeight="Bold" FontSize="14" FontFamily="Arial" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Content="{Binding BrandName}"/>
</Grid>
<Button Name="NavButton" Width="161" Height="42" ClickMode="Press" Command="{Binding CmdDrillDown, Source={StaticResource brandViewModelViewSource}}" CommandParameter="{Binding BrandName}" Content="{Binding ProdCount}" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalContentAlignment="Center" FontSize="14" FontFamily="Arial" >
</Button>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication1
{
public class BrandViewModel : INotifyPropertyChanged
{
public ObservableCollection<BrandInfoViewModel> BrandDisplayList
{
get
{
return this.Get<ObservableCollection<BrandInfoViewModel>>("BrandDisplayList");
}
set
{
this.Set<ObservableCollection<BrandInfoViewModel>>("BrandDisplayList", value);
}
}
private IDictionary<string, object> values = new Dictionary<string, object>();
protected T Get<T>(string key)
{
object v;
if (values.TryGetValue(key, out v))
return (T)v;
return default(T);
}
protected T Get<T>(string key, Func<T> defaultaction)
{
var v = this.Get<T>(key);
if (v == null)
{
v = defaultaction();
this.Set<T>(key, v);
}
return v;
}
protected void Set<T>(string key, T value)
{
object v;
values.TryGetValue(key, out v);
if (v == null || !v.Equals(value))
{
this.values[key] = value;
this.NotifyPropertyChanged(key);
}
}
public event PropertyChangedEventHandler PropertyChanged;
internal protected void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class BrandInfoViewModel
{
}
}
BrandViewmodel.cs中的相关viewmodel如下所示:
<Window x:Class="WpfApplication1.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:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<CollectionViewSource x:Key="brandViewModelViewSource" d:DesignSource="{d:DesignInstance {x:Type local:BrandViewModel}, CreateList=True}"/>
</ResourceDictionary>
</Window.Resources>
<Grid DataContext="{StaticResource brandViewModelViewSource}" x:Name="MainContainer">
<ListBox Margin="2,120,75,0" ScrollViewer.VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Width="950" Background="White" x:Name="brandDataGrid" ItemsSource="{Binding BrandDisplayList}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Name="border" BorderBrush="LightGray" BorderThickness="1" CornerRadius="7"
Padding="0" Margin="5,15,5,15">
<StackPanel Orientation="Vertical" Width="162" >
<Grid Height="42" Width="161">
<Label FontWeight="Bold" FontSize="14" FontFamily="Arial" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Content="{Binding BrandName}"/>
</Grid>
<Button Name="NavButton" Width="161" Height="42" ClickMode="Press" Command="{Binding CmdDrillDown, Source={StaticResource brandViewModelViewSource}}" CommandParameter="{Binding BrandName}" Content="{Binding ProdCount}" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalContentAlignment="Center" FontSize="14" FontFamily="Arial" >
</Button>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication1
{
public class BrandViewModel : INotifyPropertyChanged
{
public ObservableCollection<BrandInfoViewModel> BrandDisplayList
{
get
{
return this.Get<ObservableCollection<BrandInfoViewModel>>("BrandDisplayList");
}
set
{
this.Set<ObservableCollection<BrandInfoViewModel>>("BrandDisplayList", value);
}
}
private IDictionary<string, object> values = new Dictionary<string, object>();
protected T Get<T>(string key)
{
object v;
if (values.TryGetValue(key, out v))
return (T)v;
return default(T);
}
protected T Get<T>(string key, Func<T> defaultaction)
{
var v = this.Get<T>(key);
if (v == null)
{
v = defaultaction();
this.Set<T>(key, v);
}
return v;
}
protected void Set<T>(string key, T value)
{
object v;
values.TryGetValue(key, out v);
if (v == null || !v.Equals(value))
{
this.values[key] = value;
this.NotifyPropertyChanged(key);
}
}
public event PropertyChangedEventHandler PropertyChanged;
internal protected void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class BrandInfoViewModel
{
}
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间WpfApplication1
{
公共类BrandViewModel:INotifyPropertyChanged
{
公共可观察收集品牌显示列表
{
得到
{
返回此.Get(“BrandDisplayList”);
}
设置
{
此.Set(“BrandDisplayList”,值);
}
}
私有IDictionary值=新字典();
受保护的T Get(字符串键)
{
对象五;
if(值TryGetValue(键,输出v))
返回(T)v;
返回默认值(T);
}
受保护的T Get(字符串键,Func defaultaction)
{
var v=this.Get(键);
如果(v==null)
{
v=defaultaction();
此。设置(键,v);
}
返回v;
}
受保护的无效集(字符串键,T值)
{
对象五;
TryGetValue(键,输出v);
如果(v==null | |!v.Equals(value))
{
此.values[键]=值;
此.NotifyPropertyChanged(键);
}
}
公共事件属性更改事件处理程序属性更改;
内部保护的void NotifyPropertyChanged(字符串propertyName)
{
if(this.PropertyChanged!=null)
{
this.PropertyChanged(this,newpropertychangedventargs(propertyName));
}
}
}
公共类BrandInfoViewModel
{
}
}
有人知道如何在VS2015中避免此类错误吗