在针对Combobox进行datanbound时清除BindableCollection时出现问题

时间:2011-09-01 18:52:57

标签: caliburn.micro

我使用Caliburn micro,我有一个问题,如果我清除组合框被数据绑定到的列表,框架将通过异常(找不到System.String的视图)。它没有绑定到String而是ViewModel

的Xaml

<ComboBox x:Name="Mappings" MinWidth="200"></ComboBox>

模型

public BindableCollection<MappingSettingModel> Mappings { get; set; }
public MappingSettingModel SelectedMapping
{
    get { return selectedMapping; }
    set
    {
        selectedMapping = value;
        NotifyOfPropertyChange(() => SelectedMapping);
    }
}

如果我将COmbobox更改为ListView或ItemsControl,它会起作用,为什么我在使用组合框时会出错?

如果我删除了SelectedMapping属性,它可以工作,但是我需要这样才能设置应该选择哪个itemn ..

1 个答案:

答案 0 :(得分:0)

我的ViewModel:

[Export(typeof(MiscViewModel))]
    public class MiscViewModel : ScreenEx
    {
        public MiscViewModel()
        {
            DisplayName = "MiscViewModel Sample";
        }

        private BindableCollection<MyItem> _myItems;
        public BindableCollection<MyItem> MyItems
        {
            get { return _myItems; }
            set
            {
                _myItems = value;
                NotifyOfPropertyChange(() => MyItems);
            }
        }

       // public BindableCollection<MyItem> MyItems { get; set; }

        private MyItem _selectedMyItem;
        public MyItem SelectedMyItem
        {
            get { return _selectedMyItem; }
            set
            {
                _selectedMyItem = value;
                NotifyOfPropertyChange(() => SelectedMyItem);
            }
        }

        public void Load()
        {
            MyItems = new BindableCollection<MyItem>
                          {
                              new MyItem
                                  {
                                      MyItemName = "Item 1",
                                      MyItemData = "test1"
                                  },
                              new MyItem
                                  {
                                      MyItemName = "Item 2",
                                      MyItemData = "test2"
                                  },
                              new MyItem
                                  {
                                      MyItemName = "Item 3",
                                      MyItemData = "test3"
                                  },
                              new MyItem
                                  {
                                      MyItemName = "Item 4",
                                      MyItemData = "test4"
                                  }
                          };
        }

        public void Clear()
        {
            MyItems.Clear();
        }
    }

我的观点:

<UserControl x:Class="CMDemo.Client.Views.MiscView"
             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"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Test ComboBox" />
        <ComboBox x:Name="MyItems"
                  DisplayMemberPath="MyItemName"
                  Height="30"
                  VerticalAlignment="Top" />
        <Button x:Name="Load"
                Height="30"
                Content="Load"
                VerticalAlignment="Top" />
        <Button x:Name="Clear"
                Height="30"
                Content="Clear"
                VerticalAlignment="Top" />
        <TextBlock Text="Selected Item:" />
        <TextBlock x:Name="SelectedMyItem_MyItemName" />
    </StackPanel>
</UserControl>