使用MVVM将DataGrid中的ComboBox绑定到ObservableCollection的子类

时间:2014-07-11 12:35:59

标签: wpf mvvm datagrid combobox observablecollection

我正在编写一个MVVM设计的wpf应用程序。在视图中,我有一个带有组合框列的数据网格。数据网格绑定到类" AdvancedSearchQueryObjType"的ObservableCollection。我想在这个数据网格中绑定Combobox,并列出类" ObjTypeList"。我有" ObjTypeList"的列表在" AdvancedSearchQueryObjType"并在视图模型的负载上填充。

但是我在填充组合框时遇到了困难。请参阅下面的代码:

查看:

<DataGrid Name="dgSearchQuery" 
    ItemsSource="{Binding Path=QueryWindowDetails,Mode=TwoWay}" 
    Height="170" AutoGenerateColumns="False" 
    SelectionMode="Single" SelectionUnit="FullRow" 
    CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
    <DataGridTemplateColumn Header="Category" Width="150">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox DataContext="{Binding 
                 RelativeSource={RelativeSource datatyp:AdvancedSearchQueryObjType}}"
                 ItemsSource="{Binding Path=ObjTypeList}" Width="140">

                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="25" />
                                <ColumnDefinition Width="115" />
                            </Grid.ColumnDefinitions>
                            <Rectangle Grid.Column="0"  Margin="3,0,0,0" 
                                Fill="{Binding Path=Icon, Converter={StaticResource   ImgConv}, 
                                RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" />
                            <Label Grid.Column="1" Content="{Binding Path=Value, 
                                 RelativeSource=
                                {RelativeSource  AncestorType=ComboBoxItem}}" Margin="10,0,0,0"/>
                       </Grid>
                    </DataTemplate>
               </ComboBox.ItemTemplate>
           </ComboBox>
       </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

查看型号:

public class AddQueryViewModel : ViewModelBase 
{
    private void init() 
    {
        QueryWindowDetails = new ObservableCollection<AdvancedSearchQueryObjType>();
        var types = ERUSDataProvider.Instance.GetAllObjectTypeWithAttribute();
        ObjTypeList = new ObservableCollection<ObjektType>();

        types.ToList().ForEach(o => ObjTypeList.Add(new ObjektType { Key = o.Key,                                                                Value = o.Value,
                                     Image = o.Image, 
                                         Icon = o.Icon }));

        _rowCnt = 0;
        AddRow();
        _currentRow = new AdvancedSearchQueryObjType();
    }

    private void AddRow() 
    {
        RowObject = new AdvancedSearchQueryObjType();
        QueryWindowDetails.Add(RowObject);
        this.ObjTypeList.ToList().ForEach(o => RowObject.ObjTypeList.Add((ObjektType)o));
        _rowCnt += 1;
        RowObject.Id = _rowCnt;
    }
}

public class AdvancedSearchQueryObjType : ViewModelBase 
{
    private ObservableCollection<ObjektType> _objTypeList;
    private ObservableCollection<AttribType> _attribTypeList;
    private ObservableCollection<Operator> _operators;
    private ObservableCollection<bool> _conjunction;
    private bool _isNotDate;
    private bool _isNotValue;
    private Visibility _attribVisibility;

    public int Id { get; set; }

    public ObservableCollection<ObjektType> ObjTypeList 
    { 
        get { return _objTypeList; } 
        set { _objTypeList = value;  OnPropertyChanged("ObjTypeList"); } 
    }

    public ObservableCollection<AttribType> AttribTypeList { 
        get { return _attribTypeList; } 
        set { _attribTypeList = value; 
              OnPropertyChanged("AttribTypeList"); } 
    }

    public ObservableCollection<Operator> Operators { 
        get { return _operators; } 
        set { _operators = value; 
          OnPropertyChanged("Operator"); } 
    }

    public ObservableCollection<bool> Conjunction { 
        get { return _conjunction; } 
        set { _conjunction = value; 
            OnPropertyChanged("Conjunction"); } 
    }

    public bool IsNotDate {
        get { return _isNotDate; }
        set { _isNotDate = value; 
            OnPropertyChanged("IsNotDate");}
    }

    public bool IsNotValue{
        get { return _isNotValue;}
        set { _isNotValue = value;
            OnPropertyChanged("IsNotValue");}
    }

    public Visibility AttribVisibility {
        get { return _attribVisibility; }
        set {_attribVisibility = value;
            OnPropertyChanged("AttribVisibility");
        }
    }

    public AdvancedSearchQueryObjType() {
        ObjTypeList = new ObservableCollection<ObjektType>();
        AttribTypeList = new ObservableCollection<AttribType>();
        Operators = new ObservableCollection<Operator>();
        Conjunction = new ObservableCollection<bool>();
        IsNotDate = true;
        IsNotValue = false;
    }
}

请告诉我我在哪里做错了?

先谢谢

1 个答案:

答案 0 :(得分:0)

只需删除组合框中的DataContext绑定。

在datagrid列模板中,默认绑定指向ItemsSource中的当前项:

<ComboBox ItemsSource="{Binding Path=ObjTypeList}" Width="140" />

但是,由于您的DataTemplate用于编辑(组合框是一个输入控件),您应该将其放在<DataGridTemplateColumn.CellEditingTemplate>而不是<DataGridTemplateColumn.CellTemplate>

<DataGridTemplateColumn.CellTemplate>中,您只需显示当前值:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Path=SelectedObjType}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <ComboBox SelectedItem="{Binding Path=SelectedObjType}" ItemsSource="{Binding Path=ObjTypeList}" Width="140" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

假设,当前值(组合框中的选择)是SelectedObjType类中名为AdvancedSearchQueryObjType的属性。

所有这些都在documentation绑定到数据部分中得到了很好的解释:

  

数据网格中的每一行都绑定到数据源中的一个对象,数据网格中的每一列都绑定到数据对象的属性。

看看http://wpftutorial.net/DataGrid.html

相关问题