简体   繁体   中英

WPF datagridtextcolumn - always show textbox

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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