views:

165

answers:

1

Hello, By default the WPF datagridtext appears as a label and enters an edit state upon clicking. Is there a way to modify the column so that the textbox is always visible (instead of depending on the click event)? Thanks in advance, JP

+1  A: 

I updated my answer based on your clarification in your comment. You can set the template yourself for cells. Below is a sample where the age column uses textblocks.

XAML:

<Window x:Class="GridTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Height="300" Width="300">
    <StackPanel>
        <Controls:DataGrid Name="dataGrid" AutoGenerateColumns="False" >
            <Controls:DataGrid.Columns>
                <Controls:DataGridTextColumn 
                    Header="Name" 
                    Binding="{Binding Path=Name}" />
                <Controls:DataGridTemplateColumn Header="Age">
                    <Controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Age}" />
                        </DataTemplate>
                    </Controls:DataGridTemplateColumn.CellTemplate>
                    <Controls:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Age}" />
                        </DataTemplate>
                    </Controls:DataGridTemplateColumn.CellEditingTemplate>
                </Controls:DataGridTemplateColumn>
            </Controls:DataGrid.Columns>
        </Controls:DataGrid>
    </StackPanel>
</Window>

Code behind:

using System;
using System.Collections.Generic;
using System.Windows;

namespace GridTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            dataGrid.ItemsSource = new List<Person>(
                new Person[]
                {
                    new Person("Bob", 30),
                    new Person("Sally", 24),
                    new Person("Joe", 17)
                });
        }
    }

    public class Person
    {
        public String Name { get; set; }
        public int Age { get; set; }

        public Person(String name, int age)
        {
            Name = name;
            Age = age;
        }
    }
}
Wallstreet Programmer
No, I'm saying that the datagridtextcolumn is multistate. The first state is label. Clicking on the label enables data input. Losing focus switches it back to label. You only see the textbox itself when editing a particular row - i would always like to see the texbox
JP