C# 有没有办法在运行时更改GridViewColumn的CellTemplate?

C# 有没有办法在运行时更改GridViewColumn的CellTemplate?,c#,wpf,listview,datatemplate,celltemplate,C#,Wpf,Listview,Datatemplate,Celltemplate,在XAML中,在Windows.Resources中,我有两个数据模板 <Window.Resources> <DataTemplate x:Key="HorribleTemplate"> (...Horrible Stuff here ) </DataTemplate> <DataTemplate x:Key="AwesomeTemplate"> (...Awesome Stuff here ) </DataTempla

在XAML中,在Windows.Resources中,我有两个数据模板

<Window.Resources>

<DataTemplate x:Key="HorribleTemplate">
    (...Horrible Stuff here )
</DataTemplate>

<DataTemplate x:Key="AwesomeTemplate">
    (...Awesome Stuff here )
</DataTemplate>

</Window.Resources>

(…这里有可怕的东西)
(…这里的东西太棒了)

我创建了一个ListView控件,默认情况下它的CellTemplate使用ThorbleMplate。有没有办法在运行时将Listview的CellTemplate更改为AwesomeTemplate?

使用触发器时,需要使用Style设置初始属性值,否则新值将不优先,它将继续使用旧值。发生这种情况是因为属性值优先

使用触发器:

<Window x:Class="WpfListBox._32674841.Win32674841"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Win32674841" Height="300" Width="300">

    <Window.Resources>

        <DataTemplate x:Key="HorribleTemplate" DataType="{x:Type sys:String}">
                <TextBlock Background="CadetBlue" Text="{Binding}"/>
        </DataTemplate>

        <DataTemplate x:Key="AwesomeTemplate" DataType="{x:Type sys:String}">
            <TextBlock Background="Aqua" Text="{Binding}"/>
        </DataTemplate>

    </Window.Resources>

    <StackPanel>
        <Button Content="Change" Click="Button_Click"/>
        <ListView x:Name="ListView1">
            <ListView.Style>
                <Style TargetType="ListView">
                    <Setter Property="ItemTemplate" Value="{StaticResource HorribleTemplate}"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="ItemTemplate" Value="{StaticResource AwesomeTemplate}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.Style>
        </ListView>
    </StackPanel>

</Window>

从代码隐藏:

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

namespace WpfListBox._32674841
{
    /// <summary>
    /// Interaction logic for Win32674841.xaml
    /// </summary>
    public partial class Win32674841 : Window
    {
        public Win32674841()
        {
            InitializeComponent();

            ListView1.ItemsSource = DataStore.Names;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ListView1.ItemTemplate = (DataTemplate)this.Resources["AwesomeTemplate"];
        }
    }
    public class DataStore
      {
         public static List<String> Names { get { return new List<string>() { "Anjum" }; } }
      }


}
使用系统;
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
命名空间WpfListBox.\u 32674841
{
/// 
///Win32674841.xaml的交互逻辑
/// 
公共部分类Win32674841:窗口
{
公共Win32674841()
{
初始化组件();
ListView1.ItemsSource=DataStore.Names;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
ListView1.ItemTemplate=(DataTemplate)this.Resources[“AwesomeTemplate”];
}
}
公共类数据存储
{
公共静态列表名称{get{return new List(){“Anjum”};}
}
}

是的,有几种方法。改变它的触发因素是什么?事件,数据值?@GlenThomas事件,长官。看起来很有希望!我将在后面的一个问题中尝试这一点,我可以在
中放置多个
吗?是的,您可以有多个触发器。你也试试吧。