C# 如何在组合框中仅显示不在表C中的元素

C# 如何在组合框中仅显示不在表C中的元素,c#,wpf,combobox,datagrid,C#,Wpf,Combobox,Datagrid,我只想在Combobox中显示那些表列中不存在的元素 我的桌子: <DataGrid Grid.Row="1" ItemsSource="{Binding MainRows, ValidatesOnDataErrors=True}" CanUserAddRows="True" > <DataGrid.Columns> <DataGridTemplateColumn Width="2*" Header="Agent" > &l

我只想在Combobox中显示那些表列中不存在的元素

我的桌子:

<DataGrid Grid.Row="1" ItemsSource="{Binding MainRows, ValidatesOnDataErrors=True}"
  CanUserAddRows="True"
  >
<DataGrid.Columns>
    <DataGridTemplateColumn Width="2*"  Header="Agent"  >
        <DataGridTemplateColumn.CellTemplate >
            <DataTemplate>
                <ComboBox  
                    SelectedItem="{Binding Item, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                    ItemsSource="{Binding Path=DataContext.AvaibleAgents, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}">
                <ComboBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
用户只需添加一次代理。要从组合框中删除的项目,这些项目存在于datagrid中 我的DataContext虚拟机

public class  MainDocumentVM : CCRDocumentVM
{
    public  MainDocumentVM(CCRMainDocument innerValue) : base(innerValue)
    {
    }

public List<MainRowVM> _MainRows;
public List<MainRowVM> MainRows
{
    get
    {
        //TODO: Refactor it ASAP
        if (MainRows == null)
        {
            _ccrMainRows = (InnerValue as MainDocument).MainRows.Select(x => new MainRowVM(x)).ToList();
        }
        return _MainRows.Where(x=>x.InnerValue.IsActive).ToList();
    }
}
#region Functionality
private MainRowVM _selectedRow;

public MainRowVM SelectedRow
{
    get { return _selectedRow; }
    set
    {
        _selectedRow = value;
        OnPropertyChanged();
    }
}


public ICommand AddNewRowCommand
{
    get
    {
         return Commands.GetOrCreateCommand(() => AddNewRowCommand,(() => AddNewRow(CCRMainRow.CreateNew<MainRow>())));
    }
}


public void AddNewRow(MainRow row)
{
        (InnerValue as MainDocument).CCRMainRows.Add(row);
        if (_MainRows != null)
        {
            MainRows.Add(new MainRowVM(row));
        }
        OnPropertyChanged(nameof(MainRows));
        //OnPropertyChanged(nameof(AvaibleItems));
}

public List<Agents> AvaibleAgents
{
    get
    {
        return ManagerFactoryResolver.CurrentFactory.CCRAgentsManager.AllValues
            .ToList();
    }
}
}

我的视图行模型:

public class MainRowVM : EditableDataVM<MainRow>
{
    public MainRowVM(MainRow innervalue) : base(innervalue)
    {  }

    public Agent Agent
    {
        get
        {
            return ManagerFactoryResolver.CurrentFactory.AgentManager
                .AllValues.FirstOrDefault(x => x.Id == InnerValue.AgentID);
        }    
        set
        {
            InnerValue.AgentID = value.Id;
            OnPropertyChanged();
        }
    }

每次用户添加代理、删除代理和刚刚添加的代理时,您都需要更新VM中的AvailableAgents列表…

您应该使用在ViewModel中选择的任何逻辑过滤掉这些元素,并使用属性公开它们,例如,一个名为ExcludedItems的属性,然后将ComboBox.ItemsSource绑定到该属性。每次更新集合/表时,都会引发对该属性所做的更改,以反映组合框中的更改


不过需要注意的是,在绑定时,不一定要使用DataContext。前缀,仅属性本身,因为当前DataContext始终是数据上下文。

是否使用MVVM?请发布您的ViewModel代码。您的问题太广泛了。但是您应该查看AvailableAgents集合的CollectionViewSource,这样您就可以实施筛选。对不起,现在我正在尝试添加一些VM。如果我从combobox列表中的集合中删除项,这不能在视图中显示在combobox中,并引发异常,您的建议看起来就像我需要的。您得到的异常是什么?如果它们绑定到行,我将从itemsource项中删除。。我得到了一些类似的结果,如所选项目为空,因为如果它添加到datagrid,它将从数据源中删除。矛盾的人