开发者

WPF datagridtextcolumn - always show textbox

开发者 https://www.devze.com 2022-12-31 19:38 出处:网络
By default the WPF d开发者_运维知识库atagridtext 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 depend

By default the WPF d开发者_运维知识库atagridtext 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;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消