Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datagrid edit cell on mouse double click

In WPF i've added a 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?

like image 473
Tom Avatar asked Mar 18 '14 11:03

Tom


2 Answers

You need to add DataColumns in the DataGrid

<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.

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.

like image 160
Vikram Bose Avatar answered Nov 06 '22 02:11

Vikram Bose


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

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.

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>
like image 38
Sajeetharan Avatar answered Nov 06 '22 01:11

Sajeetharan