当AutoGenerateColumns =“True”时,使datagrid的特定列成为组合框

时间:2015-02-19 12:52:19

标签: c# wpf data-binding datagridcomboboxcolumn

我知道以前曾经问过这个问题,但我仍然无法让它发挥作用 我只是在测试一些东西,因为我是新手,所以我没有使用MVVM。 我有一个绑定到Observable实体集合的数据网格 数据绑定工作正常,但我想在编辑模式下将一列作为ComboBox 我实现了但我无法将该列绑定到实体的特定属性。

这是我的Datagrid xaml:

        <DataGrid x:Name="dataGrid1"   IsSynchronizedWithCurrentItem="false"  ItemsSource="{Binding}"  RowHeaderWidth="0"  HorizontalScrollBarVisibility="Disabled"  HorizontalAlignment="Left" Margin="50.752,160.516,0,0" VerticalAlignment="Top" Height="Auto"  MaxHeight="200"  Width="395.429" RenderTransformOrigin="0.5,0.5" Background="#FFCFCFCF"  ColumnWidth="*" HorizontalGridLinesBrush="Black" VerticalGridLinesBrush="Black" RowBackground="#FFCFCFCF" AreRowDetailsFrozen="True" Style="{DynamicResource DataGridStyle2}" CellEditEnding="dataGrid1_CellEditEnding" CurrentCellChanged="dataGrid1_CurrentCellChanged" Grid.Column="1" Opacity="0" SelectionChanged="dataGrid1_SelectionChanged" AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn" >
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Block.TextAlignment" Value="Center"/>
                <Setter Property="Template" Value="{DynamicResource DataGridCellControlTemplate1}"/>
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.Effect>
            <DropShadowEffect BlurRadius="20"/>
        </DataGrid.Effect>
        <DataGrid.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
            </TransformGroup>
        </DataGrid.RenderTransform>
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Background" Value="{StaticResource PrimaryBrush}"/>
                <Setter Property="Foreground" Value="{DynamicResource PrimaryFont}" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>
        </DataGrid.ColumnHeaderStyle>
        <DataGrid.Columns >

            <DataGridTemplateColumn x:Name="champ_delete" d:IsHidden="True" >
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <Button x:Name="delete" Content="Button" Width="30" Height="30" Style="{DynamicResource ButtonStyle12}" Click="supprime_Click"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>


        </DataGrid.Columns>




    </DataGrid>

这是我的MainWindow.xaml.cs代码

/// <summary>
/// Interaction logic for MENU.xaml
/// </summary>
public partial class MENU : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public MENU()
    {         
        this.InitializeComponent();            

        SContext = new Entities();

        ObsCollection = new ObservableCollection<PERFCONTENEUR>(SContext.PERFCONTENEUR.ToList());         

        DataContext = this;
        dataGrid1.ItemsSource = ObsCollection;
    }

    public BindingList<PERFCONTENEUR> Ji { get; set; }
    public ObservableCollection<PERFCONTENEUR> ObsCollection { get; set; }

    public Entities SContext;        

    private void dataGrid1_AutoGeneratingColumn(object sender, System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs e)
    {

      if (e.PropertyName == "CLIENT")
        {
          var cb = new DataGridComboBoxColumn();

          e.Column.Header = "clients";
          cb.ItemsSource = (DataContext as ObservableCollection<PERFCONTENEUR>).ToList(); // I think the problem is here

          cb.SelectedValueBinding = new Binding("CLIENT");
          e.Column = cb;
         }  
    }
  }       

1 个答案:

答案 0 :(得分:0)

请参阅以下实施。

<DataGrid  ItemsSource="{Binding Persons}" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"/>
 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }

    private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (e.PropertyName == "FirstName")
        {
            DataGridComboBoxColumn cboclm = new DataGridComboBoxColumn();
            cboclm.Header = "First Name";
            cboclm.ItemsSource = (DataContext as ViewModel).Names;
            cboclm.SelectedValueBinding = new Binding("FirstName");
            e.Column = cboclm;
        } 
    }
}
 class ViewModel
{
    private ObservableCollection<Person> persons = new ObservableCollection<Person>();

    public ObservableCollection<Person> Persons
    {
        get { return persons; }
        set { persons = value; }
    }

    private ObservableCollection<string> names = new ObservableCollection<string>();

    public ObservableCollection<string> Names
    {
        get { return names; }
        set { names = value; }
    }   

    public ViewModel()
    {
        Person p = new Person();
        persons.Add(p);
        Names.Add("Test1");
        Names.Add("Test2");
    }

}

class Person
{
    private string firstName;

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }       

}