【问题标题】:How to display in the combo box only those elements that are not in the table C#如何仅在组合框中显示那些不在表格中的元素 C#
【发布时间】:2017-09-22 02:48:36
【问题描述】:

我只想在 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>

用户只能添加一次代理。想要从组合框项目中删除,这些项目存在于数据网格中 我的 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();
        }
    }

【问题讨论】:

  • 你在使用 MVVM 吗?请同时发布您的 ViewModel 代码
  • 你的问题太宽泛了。但是您应该为您的AvaibleAgents 集合查看CollectionViewSource,以便进行过滤。
  • 抱歉,我现在尝试添加一些虚拟机。你的建议看起来像我所需要的

标签: c# wpf combobox datagrid


【解决方案1】:

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

有几点需要注意 - 绑定时,您不必使用 DataContext. 前缀,只需使用属性本身,因为当前 DataContext 始终是数据上下文。

【讨论】:

  • 如果我从组合框列表中的集合中删除项目,这不能显示在视图的组合框中,并抛出异常。
  • 你得到了什么异常?
  • 如果它们绑定到行,我会从 itemsource 项目中删除。我会得到一些类似“选定项目为空”的信息,因为如果它添加到数据网格,它将从数据源中删除。悖论)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    相关资源
    最近更新 更多