简体   繁体   English

如何更改用于GridViewColumn的CellTemplate的数据模板

[英]How to change the Datatemplate which is used for CellTemplate of the GridViewColumn

How to change the Datatemplate which is used for CellTemplate of the GridViewColumn want to change to other datatemplate so that user can edit the value of the first column only when particular row is selected and focus is on textbox 如何更改用于GridViewColumn的CellTemplate的Datatemplate想要更改为其他datatemplate,以便仅当选中特定行并且焦点位于文本框上时,用户才可以编辑第一列的值

I have created a sample app to reproduce the problem. 我创建了一个示例应用程序来重现该问题。

Here is the code in xaml file: 这是xaml文件中的代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">
    <Window.Resources>

        <Style TargetType="{x:Type TextBox}" >
            <Setter Property="BorderThickness" Value="0"/>
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="IsReadOnly" Value="False" />
                </Trigger>
                <Trigger Property="IsFocused" Value="False">
                    <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=Background}"/>
                    <Setter Property="IsReadOnly" Value="True" />
                </Trigger>
                <Trigger Property="IsReadOnly" Value="False">
                    <Setter Property="Background" Value="White"/>                    
                </Trigger>
            </Style.Triggers>
        </Style>

        <DataTemplate x:Key="DefaultTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="EditableTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBox Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>

        <Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
            <Setter Property="View">
                <Setter.Value>
                    <GridView >
                        <GridViewColumn Header="Name" CellTemplate="{StaticResource DefaultTemplate}"/>
                        <GridViewColumn Header="Age">
                            <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Age}"/>
                                </StackPanel>
                            </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

                    </GridView>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <StackPanel>       
        <ListView x:Name="FoldersListView" Margin="3" SelectionMode="Single"  ItemsSource="{Binding Path=ObjectCollection}"                  
                         IsSynchronizedWithCurrentItem="True" Style="{StaticResource ListViewStyle}">

        </ListView>
     </StackPanel>

</Window>

Below is in xaml.cs file 下面是xaml.cs文件中

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{

    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public testVM VM = new testVM();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = VM;
        }     
    }

    public class testVM : ViewModelBase
    {
        List<emplyee> myProperty = new List<emplyee>();

        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged("Title");
            }
        }

        public testVM()
        {
            objectCollection.Add(new emplyee() { Name = "aaa India",Age="10" });
            objectCollection.Add(new emplyee() { Name = "bbb India",Age="20" });
            objectCollection.Add(new emplyee() { Name = "ccc India",Age="30" });
            objectCollection.Add(new emplyee() { Name = "ddd India",Age="40" });
        }

        public List<emplyee> MyProperty { get{return myProperty;} }

        private ObservableCollection<emplyee> objectCollection = new ObservableCollection<emplyee>();
        public ObservableCollection<emplyee> ObjectCollection
        {
            get
            {


                return objectCollection;
            }
            set
            {
                objectCollection = value;

            }
        }


    }

    public class emplyee
    {
        public emplyee()
        {

        }

        public string Name { get; set; }
        public string Age { get; set; }

    }
}

How to select the datatemplate dynamically? 如何动态选择数据模板?

For selecting data templates dynamically, use the DataTemplateSelector class. 要动态选择数据模板,请使用DataTemplateSelector类。 You must override its SelectTemplate method. 您必须重写其SelectTemplate方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有一种方法可以在运行时更改GridViewColumn的CellTemplate? - Is there a way to change CellTemplate of GridViewColumn at Runtime? WPF GridViewColumn CellTemplate c#的动态更改 - WPF Dynamic change of GridViewColumn CellTemplate c# 如何创建一个上下文菜单,其中包含Gridviewcolumn.celltemplate中的菜单项? 无法获取示例代码 - How to Create a context menu which contains menu items in a Gridviewcolumn.celltemplate? Unable to get sample code to work 从 GridViewColumn 获取 CellTemplate 绑定 - Get CellTemplate binding from GridViewColumn WPF 如何更改 gridviewcolumn 的颜色 - WPF How to change the color of a gridviewcolumn 将DataTemplate绑定到DataGridColumn CellTemplate - Binding DataTemplate to DataGridColumn CellTemplate 使用celltemplate绑定集合以显示在gridview列中 - Binding a collection for display in a gridviewcolumn using celltemplate 从 GridViewColumn 或 GridViewColumn.CellTemplate 的 GridView 获取项目 - Get items from GridViewColumn or GridViewColumn.CellTemplate of GridView GridViewColumn CellTemplate 文本运行的条件可见性 - GridViewColumn CellTemplate Conditional Visibility of Text Run 当CellEditingTemplate DataTemplate控件更改其绑定值时,如何更新CellTemplate DataTemplate控件? - How do I update the CellTemplate DataTemplate control when the CellEditingTemplate DataTemplate control changes its bound value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM