WPF Datagrid 鼠标双击编辑单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22477695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datagrid edit cell on mouse double click
提问by Tom
In WPF i've added a DataGrid:
在 WPF 中,我添加了一个 DataGrid:
<DataGrid x:Name="dataGridProdotti" HorizontalAlignment="Left" Margin="10,56,0,0" VerticalAlignment="Top" Height="250" Width="426" SelectionChanged="dataGridProdotti_SelectionChanged" IsReadOnly="False"/>
with the property
与财产
IsReadOnly="False"
Then i do:
然后我做:
dataGridProdotti.ItemsSource = myList
Why if i double click on a cell, that cell doesn't go in the edit mode?
为什么如果我双击一个单元格,该单元格不会进入编辑模式?
回答by Vikram Bose
You need to add DataColumns in the DataGrid
您需要在 DataGrid 中添加 DataColumns
<DataGrid x:Name="dataGridProdotti"
HorizontalAlignment="Left"
ItemsSource="{Binding Values}"
Margin="10,10,0,192" Width="481" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="False" Binding="{Binding Path=Name}" Header="List" />
</DataGrid.Columns>
</DataGrid>
And also Dont bind the list<string>directly to datasource of the DataGrid, Create one custom class and then bind like below.
并且不要list<string>直接绑定到 DataGrid 的数据源,创建一个自定义类,然后像下面这样绑定。
private List<Country> value = new List<Country>();
public MainWindow()
{
InitializeComponent();
this.Values.Add(new Country{ Name = "America"});
this.Values.Add(new Country{Name = "Africa"});
this.Values.Add(new Country{Name = "India"});
}
public List<Country> Values
{
get
{
return this.value;
}
set
{
this.value = value;
}
}
}
public class Country
{
public string Name { get; set; }
}
Now the DataGrid is editable.
现在 DataGrid 是可编辑的。
回答by Sajeetharan
I dont see you having any columns in your datagrid,
我没有看到你的数据网格中有任何列,
Just drop a DataGrid control to your view and bind the ItemsSource to a collection of data objects and you're done. The DataGrid provides a feature called AutoGenerateColumns that automatically generates column according to the public properties of your data objects
只需将 DataGrid 控件拖放到您的视图中,并将 ItemsSource 绑定到数据对象的集合,您就完成了。DataGrid 提供了一个称为 AutoGenerateColumns 的功能,它根据数据对象的公共属性自动生成列
Alternatively you can define your columns manually by setting the AutoGenerateColumns property to False. In this case you have to define the columns in the Columns collection of the data grid.
或者,您可以通过将 AutoGenerateColumns 属性设置为 False 来手动定义列。在这种情况下,您必须在数据网格的 Columns 集合中定义列。
If you want to edit your datagrid cell, you should define a datatemplateColumn,
如果你想编辑你的数据网格单元,你应该定义一个数据模板列,
<sdk:DataGridTemplateColumn Header="Yourheadername" Width="150" CanUserResize="False" CanUserReorder="False">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="2" VerticalAlignment="Center" x:Name="txtblock" Text="{Binding Test,Mode=TwoWay}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

