简体   繁体   English

有没有一种方法可以在运行时更改GridViewColumn的CellTemplate?

[英]Is there a way to change CellTemplate of GridViewColumn at Runtime?

In XAML, inside Windows.Resources, i have 2 DataTemplate 在XAML中,在Windows.Resources中,我有2个DataTemplate

<Window.Resources>

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

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

</Window.Resources>

I created a ListView control, which use the HorribleTemplate for its CellTemplate by default. 我创建了一个ListView控件,默认情况下将HorribleTemplate用作其CellTemplate。 Is there a way to change the CellTemplate of the Listview to AwesomeTemplate at Runtime? 有没有一种方法可以在运行时将Listview的CellTemplate更改为AwesomeTemplate?

While using Triggers, you need to set initial property value using Style, otherwise new value won't take precedence, and it will keep using Old value. 使用触发器时,您需要使用样式设置初始属性值,否则新值将不会优先,并且它将继续使用旧值。 This happens because of Property value precedence. 这是因为属性值优先。

Using Triggers : 使用触发器:

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

From code-behind : 从后台代码:

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


}

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

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