C#WPF - 数据网格中的可编辑列

时间:2015-06-02 21:07:39

标签: c# wpf xaml wpfdatagrid

问题描述

我有一个名为 data_grid_1 DataGrid ,它有两列 header1 header2 ,并且填充了< strong> SomeClass -object位于data_grid_1 Loaded-event-handler中。

XAML的代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
     <Grid>
        <DataGrid x:Name="data_grid_1"
                  HorizontalAlignment="Left"
                  Margin="10,10,0,0" 
                  VerticalAlignment="Top"
                  Height="303" Width="497" 
                  Loaded="data_grid_1_Loaded">
            <DataGrid.Columns>
                <DataGridTextColumn Header="header1" 
                                    Binding="{Binding some_field_variable_2, Mode=OneWay}" />
                <DataGridTextColumn Header="header2" 
                                    Binding="{Binding some_field_variable_3, Mode=OneWay}" />
            </DataGrid.Columns>
     </DataGrid>
</Grid>
</Window>

XAML.CS码:

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void data_grid_1_Loaded(object sender, RoutedEventArgs e)
        { 
            data_grid_1.Items.Add(new SomeClass(1, "data_grid_1-String1", "data_grid_1-Text1"));
            data_grid_1.Items.Add(new SomeClass(2, "data_grid_1-String2", "data_grid_1-Text2"));
            data_grid_1.Items.Add(new SomeClass(3, "data_grid_1-String3", "data_grid_1-Text3"));
            data_grid_1.Items.Add(new SomeClass(4, "data_grid_1-String4", "data_grid_1-Text4"));
        }
    }
}

SomeClass.cs

namespace WpfApplication1
{
class SomeClass
{
    public int some_field_variable_1 { get; internal set; }
    public string some_field_variable_2 { get; internal set; }
    public string some_field_variable_3 { get; internal set; }

    public SomeClass(int some_field_variable_1,
             string some_field_variable_2,
             string some_field_variable_3)
    {
        this.some_field_variable_1 = some_field_variable_1;
        this.some_field_variable_2 = some_field_variable_2;
        this.some_field_variable_3 = some_field_variable_3;
    }
}
}

问题

我想要 header2-column editable ,因此如果用户点击并更改header2-column中某行的值,则更改将写入SomeClass-object,之前已添加在 data_grid_1_Loaded

如何使第二列可编辑?

2 个答案:

答案 0 :(得分:2)

public partial class MainWindow : Window
   {
       public MainWindow()
    {
        InitializeComponent();
    }

    public static ObservableCollection<SomeClass> GetItems()
    {
        ObservableCollection<SomeClass> some_inner_object_list = new ObservableCollection<SomeClass>();
        some_inner_object_list.Add(new SomeClass(1, "data_grid_1-String1", "data_grid_1-Text1"));
        some_inner_object_list.Add(new SomeClass(2, "data_grid_1-String2", "data_grid_1-Text2"));
        some_inner_object_list.Add(new SomeClass(3, "data_grid_1-String3", "data_grid_1-Text3"));
        some_inner_object_list.Add(new SomeClass(4, "data_grid_1-String4", "data_grid_1-Text4"));

        return some_inner_object_list;
    }
    private void data_grid_1_Loaded(object sender, RoutedEventArgs e)
    {
        data_grid_1.ItemsSource = GetItems();

    }
    private void data_grid_1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        if (e.AddedCells.Count == 0) return;
        var currentCell = e.AddedCells[0];
        if (currentCell.Column ==
            data_grid_1.Columns[1])
        {
            data_grid_1.BeginEdit();
        }
    }
}

XAML的代码:

<DataGrid x:Name="data_grid_1" 
              AutoGenerateColumns="False" 
              CanUserAddRows="False" 
              SelectionMode="Extended" 
              SelectionUnit="Cell" 
              Loaded="data_grid_1_Loaded"
              SelectedCellsChanged="data_grid_1_SelectedCellsChanged" Margin="0,12,12,77">
        <DataGrid.Columns>
            <DataGridTextColumn Header="header1" Binding="{Binding some_field_variable_2, Mode=OneWay}" Width="1*" />
            <DataGridTemplateColumn Header="header2" Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding some_field_variable_3}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <TextBox Text="{Binding some_field_variable_3}" />
                            <TextBlock Grid.Column="1" />
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>

答案 1 :(得分:0)

只使用DataGridTextColumn.CellStyle

 <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="DataGridCell">
                                    <TextBox>//Binding your logic here
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

希望它有所帮助...... :)