当 datanbound 针对 Combobox 时清除 BindableCollection 时出现问题

发布于 2024-12-02 13:13:25 字数 655 浏览 1 评论 0原文

我使用 Caliburn micro,我遇到一个问题,如果我清除组合框数据绑定到的列表,框架将出现异常(找不到 System.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 属性,它会起作用,但我需要它,以便我可以设置应选择哪个项目。

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(() => 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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2024-12-09 13:13:25

我的视图模型:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文