新行将自动添加到数据网格中

时间:2014-02-26 19:16:56

标签: c# sql wpf xaml datagrid

我在SQL Server中名为ERP_Lite的数据库中有一个名为UnitRelations的表。该表的结构如下:

Column Name        |  Data Type     |     Allow Nulls
-------------------+----------------+----------------
UnitRelationID     |  int           |     No            [Primary Key]
FirstUnitID        |  int           |     No            [Foreign Key - Units]
FirstUnitQuantity  |  float         |     No
SecondUnitID       |  int           |     No            [Foreign Key - Units]
SecondUnitQuantity |  float         |     No
ItemID             |  int           |     Yes           [Foreign Key - Items]

我使用过entity framewwork。因此,在创建ADO.Net Entity Data Model (.edmx)时,UnitRelations的名称会被归为UnitRelation

现在在我的视图模型文件中 - ItemsViewModel.cs

public class ItemsViewModel : MainViewModel
{
    public ItemsViewModel()
    {
        using(Entities db = new Entities())
        {
            Units = (from m in db.Units
                     select m).ToList();
        }

        UnitRelations = new List<UnitRelation>();
    }

    private IEnumerable<Unit> _units;
    public IEnumerable<Unit> Units
    {
        get
        {
            return _units;
        }
        set
        {
            _units = value;
            OnPropertyChanged("Units");
        }
    }

    private List<UnitRelation> _unitRelations;
    public List<UnitRelation> UnitRelations
    {
        get
        {
            return _unitRelations;
        }
        set
        {
            _unitRelations = value;
            OnPropertyChanged("UnitRelations");
        }
    }
}

这是我的Items.xaml文件:

<DataGrid ItemsSource="{Binding UnitRelations}"  AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Something" Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Text="First Unit" />
                    <ComboBox Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" 
                                  ItemsSource="{Binding DataContext.Units, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Name}" Width="100" />
                                    <TextBlock Text="{Binding Symbol}" />
                                </StackPanel>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                    <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Text="Second Unit" />
                    <ComboBox Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" 
                                  ItemsSource="{Binding DataContext.Units, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Name}" Width="100" />
                                    <TextBlock Text="{Binding Symbol}" />
                                </StackPanel>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                        <TextBox Grid.Row="2" Grid.Column="0" Text="{Binding FirstUnitQuantity}"/>
                        <TextBlock Grid.Row="2" Grid.Column="1" />
                        <TextBox Grid.Row="2" Grid.Column="2" Text="{Binding SecondUnitQuantity}"/>
                        <TextBlock Grid.Row="2" Grid.Column="3" />
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

以下是我的输出和问题的屏幕截图:

(1)最初

enter image description here

(2)当我在ComboBox中选择项目时

enter image description here

(3)当我点击DataGrid的空白区域

时从Combobox中选择项目

enter image description here

(4)当我点击DataGrid外的任何控件时,例如TextBox(数据网格顶部的新行自动添加)

[注意:如果我没有点击datagrid的空白区域,则跳过第3步,问题不再存在。]

enter image description here

(5)当我双击datagrid的最后一行时。

enter image description here

(6)当我点击任何其他行时,会再次添加一个新行,此过程会连续多次添加新行,因为我点击任何一行数据网格。

enter image description here

最后,我想了解向datagrid添加新行的原因,并停止我的应用添加这些行。

0 个答案:

没有答案