开发者

Problem clearing BindableCollection when datanbound against Combobox

I use Caliburn micro, I have a problem where the framework will through an exception (Can not find view for System.String) if i clear the list that the combobox is databound to. Its not bound to a String but a ViewModel

Xaml

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

Model

public BindableCollection<MappingSettingModel> Mappings { get; set; }
public MappingSettingModel SelectedMapping
{
    get { return selectedMapping; }
    set
    {
        selectedMapping = value;
        NotifyOfPropertyChange(() =>开发者_Python百科 SelectedMapping);
    }
}

It works if i change the COmbobox to a ListView or a ItemsControl,why do i get errors when using a combobox?

It works if i remove the SelectedMapping property, But i need that so that I can set which itemn shouldbe selected..


My 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();
        }
    }

My View:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜