c# - WPF Datagrid 鼠标双击编辑单元格

标签 c# wpf datagrid wpfdatagrid

在 WPF 中我添加了一个 DataGrid:

<DataGrid x:Name="dataGridProdotti" HorizontalAlignment="Left" Margin="10,56,0,0" VerticalAlignment="Top" Height="250" Width="426" SelectionChanged="dataGridProdotti_SelectionChanged" IsReadOnly="False"/>

属性

IsReadOnly="False"

然后我做:

dataGridProdotti.ItemsSource = myList

为什么如果我双击一个单元格,该单元格不会进入编辑模式?

最佳答案

您需要在 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>

并且不要绑定(bind) list<string>直接到 DataGrid 的数据源,创建一个自定义类,然后像下面那样绑定(bind)。

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; }
}

现在 DataGrid 是可编辑的。

关于c# - WPF Datagrid 鼠标双击编辑单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22477695/

相关文章:

wpf - DataGrid 的列宽 (width = "*") 不是 "refreshed"

c# - 没有虚拟化的 WPF DataGrid 性能

c# - 在 ASP.NET 中将行插入数据库

c# - URL 的编码参数

c# - WPF自定义控件库图片资源

wpf - WinForms - 像绘画一样的 WPF

c# - Datagrid 不刷新更改的数据

datagrid - 选择dojo 1.4网格中的所有复选框

c# - 使用 MVC5 应用程序将数据库从本地 SQL 切换到 AZURE DB

c# - 在文本文件中加载和访问模板变量