XamlObjectWriterException:Collection属性' System.Windows.Data.CollectionViewSource'。' DesignSource'一片空白

时间:2017-07-12 04:56:26

标签: c# xaml

在VS2013中,如果我们使用collectionView并设置listbox的Itemsource BindDisplayingList,则没有xaml错误。 xaml可以正确地进行二值分析。 但是,在VS2015中,如果我们使用在VS2013中运行良好的相同项目,则会出现xaml错误,指出DesignSource为null。 我只是搜索了错误,但找不到合适的答案。 xaml代码如下:

<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>

相关的viewmodel位于BrandViewmodel.cs中,如下所示:

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
    {

    }
}

有人知道如何在VS2015中避免此类错误吗?

0 个答案:

没有答案
相关问题