简体   繁体   English

鼠标双击WPF Datagrid编辑单元格

[英]WPF Datagrid edit cell on mouse double click

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? 为什么我双击一个单元格,该单元格不进入编辑模式?

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是可编辑的。

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. 只需将DataGrid控件拖放到视图中,然后将ItemsSource绑定到数据对象的集合即可。 The DataGrid provides a feature called AutoGenerateColumns that automatically generates column according to the public properties of your data objects DataGrid提供了一个名为AutoGenerateColumns的功能,可根据数据对象的公共属性自动生成列

Alternatively you can define your columns manually by setting the AutoGenerateColumns property to False. 或者,您可以通过将AutoGenerateColumns属性设置为False来手动定义列。 In this case you have to define the columns in the Columns collection of the data grid. 在这种情况下,您必须在数据网格的Columns集合中定义列。

If you want to edit your datagrid cell, you should define a datatemplateColumn, 如果要编辑datagrid单元格,则应定义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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM