Combobox和删除的记录

时间:2015-06-09 07:15:23

标签: c# wpf combobox

我有2个相关的表 - CarMarks和CarMarkGroups。任何CarMark都有CarMarkGroupID。我有CarMarks的编辑表单,其中combobox绑定到CarMarkGroups。

我将CarMarkGroup中的一个标记为已删除的记录。因此,这个标记为CarMarkGroup的列表不与combobox绑定(因为我必须只显示活动的CarMarkGroup)。但选定CarMark中的CarMarkGroupID仍会链接到标记为已删除的CarMarkGroup。我不需要改变这个链接。

但是我需要为选定的CarMark显示此记录,即使它被标记为删除。最佳做法是什么?我需要在ChangeSelection事件中构建组合框列表吗?或者是什么?

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全了解这些连接,但如果您仍然需要在CarMarks ComboBox中看到CarMarkGroup,可能会在CarMarkGroup中添加一个布尔标志,类似于ProposedForDeletion,当更改为true时,您会更改某些颜色或GUI中的另一个元素是让用户现在建议删除,甚至在某处显示有关此内容的消息。不要从ObservableCollection中删除它,只是将其标志标记为true并保持在那里并根据此新标志进行进一步检查。

答案 1 :(得分:0)

我建议基于ICollectionView过滤的解决方案 查看型号:

public class GroupViewModel
{
    public string Name { get; set; }
    public bool IsDeleted { get; set; }
}

public class ItemViewModel
{
    public ItemViewModel(List<GroupViewModel> groups)
    {
        Groups = new ListCollectionView(groups)
        {
            Filter = g =>
            {
                var group = (GroupViewModel)g;

                return !group.IsDeleted || (Group == group);
            }
        };
    }

    public string Name { get; set; }

    public GroupViewModel Group
    {
        get { return group; }
        set
        {
            if (group != value)
            {
                group = value;
                Groups.Refresh();
            }
        }
    }
    private GroupViewModel group;

    public ICollectionView Groups { get; private set; }
}

public class ListViewModel
{
    private readonly List<GroupViewModel> groups;

    public ListViewModel()
    {
        // some sample initialization
        groups = new List<GroupViewModel>
        {
            new GroupViewModel { Name = "Friuts" },
            new GroupViewModel { Name = "Animals" },
            new GroupViewModel { Name = "Trees", IsDeleted = true }
        };

        Items = new List<ItemViewModel>
        {
            new ItemViewModel(groups) { Name = "Orange", Group = groups[0] },
            new ItemViewModel(groups) { Name = "Apple", Group = groups[0] },
            new ItemViewModel(groups) { Name = "Banana", Group = groups[0] },
            new ItemViewModel(groups) { Name = "Cat", Group = groups[1] },
            new ItemViewModel(groups) { Name = "Dog", Group = groups[1] },
            new ItemViewModel(groups) { Name = "Bird", Group = groups[1] },
            new ItemViewModel(groups) { Name = "Oak", Group = groups[2] },
            new ItemViewModel(groups) { Name = "Nut-tree", Group = groups[2] },
            new ItemViewModel(groups) { Name = "Pine", Group = groups[2] },
        };
    }

    public IEnumerable<ItemViewModel> Items { get; private set; }
}

查看(它是WPF应用程序项目中的Window内容):

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <DataGrid x:Name="ItemsGrid" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Group" Binding="{Binding Group.Name}"/>
        </DataGrid.Columns>
    </DataGrid>

    <ContentControl Grid.Column="1" Content="{Binding SelectedItem, ElementName=ItemsGrid}">
        <ContentControl.ContentTemplate>
            <DataTemplate>
                <ComboBox Height="20" ItemsSource="{Binding Groups}" SelectedItem="{Binding Group}">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </DataTemplate>
        </ContentControl.ContentTemplate>
    </ContentControl>
</Grid>

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ListViewModel();
    }
}

因此,当选择项目时,ComboBox包含所有未删除的组PLUS所选项目组(如果已删除此组)。