在DataGrid中组合Combobox,隐藏Combobox上的选定项目

时间:2015-07-09 16:03:42

标签: wpf xaml mvvm datagrid combobox

我想要完成的是在DataGrid上有一个连续的组合框。每个组合框都绑定到相同的ItemsSource。我希望最终用户能够点击组合框中的项目,并且对于该特定的组合框,将该项目作为所选项目。然后其余的组合框将隐藏所选值。我仍然是XAML的新手,虽然我理解整个MVVM模式(这就是我正在使用的)。

XAML:

<DataGridTemplateColumn Header="Database Column" Width="150">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <ComboBox ItemsSource="{Binding DataContext.DatabaseMappingFields, ElementName=Root}" DisplayMemberPath="ColumnName">
        <ComboBox.Resources>
          <Style TargetType="ComboBoxItem">
            <Setter Property="Visibility" Value="{Binding IsSelected, Converter={StaticResource InverseBoolToVis}}"/>
          </Style>
        </ComboBox.Resources>
        <i:Interaction.Triggers>
          <i:EventTrigger EventName="SelectionChanged">
            <command:EventToCommand Command="{Binding DataContext.CommandDatabaseHeaderSelected, ElementName=Root}"  PassEventArgsToCommand="True"/>
          </i:EventTrigger>
        </i:Interaction.Triggers>
      </ComboBox>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

C#:

SetDatabaseColumns(new List<string>()
{ "Item1", "Item2", "Item3", "Item4", "Item5", "Item6" });

public void SetDatabaseColumns(List<string> databaseColumnNames)
    {
        DatabaseMappingFields.Clear();
        DatabaseMappingFields.Add(new DatabaseColumnsModel() { ColumnName = "<ignore>" });
        foreach (var item in databaseColumnNames)
        {
            DatabaseMappingFields.Add(new DatabaseColumnsModel() { ColumnName = item });
        }
    }

public ICommand CommandDatabaseHeaderSelected
    {
        get
        {
            return _commandDatabaseHeaderSelected ??
                   (_commandDatabaseHeaderSelected = new RelayCommand<SelectionChangedEventArgs>(DatabaseHeaderSelected));
        }
    }

private void DatabaseHeaderSelected(SelectionChangedEventArgs args)
    {
        var selectedItem = args.AddedItems.Count >= 1
            ? args.AddedItems[0] as DatabaseColumnsModel
            : null;
        var previousItem = args.RemovedItems.Count >= 1
            ? args.RemovedItems[0] as DatabaseColumnsModel
            : null;
        var selectedFileMapping = Data.TextFileMapping.FirstOrDefault(w => w == SelectedFileMapping);

        if (selectedItem == null) return;

        var isSelected = IsSelectedItemEnabled(selectedItem, previousItem);
        selectedItem.IsSelected = isSelected;

        selectedFileMapping.DatabaseColumn = isSelected ? selectedItem : null;

        if (previousItem == null) return;
        previousItem.IsSelected = false;
    }

public class DatabaseColumnsModel : ObservableObject
{
    private string _columnName;
    private bool _isSelected;
    private int _index;

    public int Index
    {
        get { return _index; }
        set { Set(() => Index, ref _index, value); }
    }

    public string ColumnName
    {
        get { return _columnName; }
        set { Set(() => ColumnName, ref _columnName, value); }
    }

    public bool IsSelected
    {
        get { return _isSelected; }
        set { Set(() => IsSelected, ref _isSelected, value); }
    }
}

1 个答案:

答案 0 :(得分:0)

由于我们正在制作一个桌面应用程序,并希望在我们的所有应用程序中拥有共同的外观和感觉,我们决定使用WPF和CefSharp并使用WPF作为shell,前端将基于HTML并使用WPF CefSharp控件,用于在WPF中显示浏览器窗口。