0

我目前有一个C# WPF .NET 4.5应用程序。我正在使用 aDataGrid来显示项目数据库中的项目。我能够填充DataGridDataGridComboBoxColumn. 首先,我只想让它显示数据。将来,我希望组合框在第一行显示数据库中的当前值,但让 ComboBox 包含设置值,以限制用户可以输入的内容。所以它就像列表 item1 = 当前值,列表 item2 = 磅,列表 item3 = 盎司等。该表有 3 列我正在使用,InventoryName、Qty 和 Type。类型应该是 ComboBox

这是我的代码

 private AuroraDataEntities auroraDataEntities;
     public ViewCup()
    {            
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        LoadData();
    }

    private void LoadData()
    {
        auroraDataEntities = new AuroraDataEntities();
        dgInv.DataContext = auroraDataEntities.Inventories;
    }

   void EditComplete(object sender, SelectedCellsChangedEventArgs e)
    {
        auroraDataEntities.SaveChanges();
    }

    private void Window_Unloaded(object sender, RoutedEventArgs e)
    {
        auroraDataEntities.SaveChanges();
    }


  }

这是我的 XAML

        <DataGrid x:Name="dgInv" HorizontalAlignment="Center" Height="134" Margin="-10,96,10,0" VerticalAlignment="Top" Width="Auto" 
              IsSynchronizedWithCurrentItem="True" 
              ItemsSource="{Binding}" 
              AutoGenerateColumns="False"  
              GridLinesVisibility="None" 
              SelectedCellsChanged="EditComplete">
        <DataGrid.Columns>
            <DataGridTextColumn Width="200" Header="InventoryName" Binding="{Binding InventoryName}" />
            <DataGridTextColumn Width="50" Header="Qty" Binding="{Binding Qty}" />
            <DataGridComboBoxColumn x:Name="cbType"
                                    Width="50" 
                                    Header="Type" 
                                    ItemsSource="{Binding Path=Type, Mode=TwoWay}" 
                                    DisplayMemberPath="Type" 
                                    SelectedValueBinding="{Binding Type}" >     
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>
4

2 回答 2

1

为了使DataGridComboBoxColumn正常工作,您必须使用ElementStyleand EditingElementStyle,如下所示:

<DataGridComboBoxColumn x:Name="cbType" Width="50" Header="Type" 
    DisplayMemberPath="Type" SelectedValueBinding="{Binding Type}" >
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="ItemsSource" Value="{Binding Type}"/>
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="ItemsSource" Value="{Binding Type}"/>
        </Style>
    </DataGridComboBoxColumn.ElemEditingElementStyleentStyle>
</DataGridComboBoxColumn>

这将ComboBox在网格列中填充您的ItemsSource. 根据您ComboBox在网格中使用 的方式,您可能不需要设置ElementStyle. 有关为什么这是必要的一些信息,请参阅此答案

于 2013-08-04T01:14:07.943 回答
0

您的 DataContext 应该有一个 ViewModel,它应该公开两个单独的属性,Inventories 和 TypeCollection(一个包含盎司、磅等的数组或列表),并且您应该将 DataGridComboBoxColumn 的 ItemsSource 绑定到 TypeCollection 并将 SelectedValueBinding 绑定到 Type。

public class ViewModel{

    // implement INotifyPropertyChanged for following
    public IEnumerable TypeCollection{ get; private set;}

    public IEnumerable Inventories {get; private set;}

}

// 如下图设置DataContext

 this.DataContext = new ViewModel(.....);

// 如下图设置绑定

    <DataGrid x:Name="dgInv" HorizontalAlignment="Center" Height="134" Margin="-10,96,10,0" VerticalAlignment="Top" Width="Auto" 
          IsSynchronizedWithCurrentItem="True" 
          ItemsSource="{Binding Inventories}" 
          AutoGenerateColumns="False"  
          GridLinesVisibility="None" 
          SelectedCellsChanged="EditComplete">
    <DataGrid.Columns>
        <DataGridTextColumn Width="200" Header="InventoryName" Binding="{Binding InventoryName}" />
        <DataGridTextColumn Width="50" Header="Qty" Binding="{Binding Qty}" />
        <DataGridComboBoxColumn x:Name="cbType"
                                Width="50" 
                                Header="Type" 
                                ItemsSource="{Binding Path=DataContext.TypeCollection,
    RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}}" 
                                DisplayMemberPath="Type" 
                                SelectedValueBinding="{Binding Path=Type, Mode=TwoWay}" >     
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>

请注意,您必须使用 RelativeSource 来查找父 DataGrid 的 DataContext。

于 2013-08-04T10:21:02.163 回答