C#WPF-数据网格中的可编辑列
本文关键字:编辑 数据 数据网 网格 C#WPF- | 更新日期: 2023-09-27 17:52:41
问题描述
我有一个名为data_grid_1的DataGrid,它有两列header1和header2,并在data_grid_1加载的事件处理程序中填充了SomeClass对象。
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列可编辑,因此,如果用户单击并更改header2列中某行的值,则更改将写入SomeClass对象,该对象以前添加在data_grid_1_Loaded中。
如何使第二列可编辑
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>
仅使用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>
希望能有所帮助…:(