WPF模板1

Template

ControlTemplate

DataTemplate

 

 

WPF模板1

 

表格数据模板:

<DataGrid x:Name="dg" AutoGenerateColumns="False">

            <DataGrid.Columns>

                <DataGridTemplateColumn Header="姓名"  Width="100" IsReadOnly="True">

                    <DataGridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <StackPanel Orientation="Horizontal">

                                <TextBlock Text="{Binding Name}" Width="100" Height="25" FontSize="20"

                                           Foreground="white" Background="#939430"

                                           VerticalAlignment="Center" TextAlignment="Left" />

                            </StackPanel>

                        </DataTemplate>

                    </DataGridTemplateColumn.CellTemplate>

                   

                </DataGridTemplateColumn>

 

                <DataGridTemplateColumn Header="年龄"  Width="100" IsReadOnly="True">

 

                    <DataGridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <StackPanel Orientation="Horizontal">

                                <Rectangle Fill="red" Width="{Binding Age}" ></Rectangle>

                                <TextBlock Text="{Binding Age}"></TextBlock>

                            </StackPanel>

                        </DataTemplate>

                    </DataGridTemplateColumn.CellTemplate>

 

                </DataGridTemplateColumn>

 

                <DataGridTemplateColumn Header="身高"  Width="250" IsReadOnly="True">

 

                    <DataGridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <StackPanel Orientation="Horizontal">

                                <Rectangle Fill="Chocolate" Width="{Binding Height}" ></Rectangle>

                                <TextBlock Text="{Binding Height}"></TextBlock>

                            </StackPanel>

                        </DataTemplate>

                    </DataGridTemplateColumn.CellTemplate>

 

                </DataGridTemplateColumn>

 

 

            </DataGrid.Columns>

        </DataGrid>

 

 

List<Student> students = new List<Student>();

            students.Add(new Student() { Name = "王伟", Age = 13, Height = 167 });

            students.Add(new Student() { Name = "赵明明", Age = 12, Height = 177 });

            students.Add(new Student() { Name = "张三", Age = 20, Height = 174 });

            students.Add(new Student() { Name = "里斯", Age = 22, Height = 180 });

 

            dg.ItemsSource = students;

 

 

public class Student

    {

        public string Name { get; set; }

        public int Height { get; set; }

        public int Age { get; set; }

 

}

 

效果:

WPF模板1

 

 

 

 

使用资源存储数据样式:

 

<Window x:Class="WpfApp1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp1"

        xmlns:test="clr-namespace:System.Data;assembly=System.Data"

        xmlns:sys="clr-namespace:System;assembly=mscorlib"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800" >

 

    <Window.Resources>

        <DataTemplate x:Key="xName" >

            <StackPanel Orientation="Horizontal">

                <TextBlock Text="{Binding Name}" Width="100" Height="25" FontSize="20"

                                           Foreground="white" Background="#939430"

                                           VerticalAlignment="Center" TextAlignment="Left" />

            </StackPanel>

        </DataTemplate>

 

        <DataTemplate x:Key="xAge">

            <StackPanel Orientation="Horizontal">

                <Rectangle Fill="red" Width="{Binding Age}" ></Rectangle>

                <TextBlock Text="{Binding Age}"></TextBlock>

            </StackPanel>

        </DataTemplate>

 

        <DataTemplate x:Key="xHeight">

            <StackPanel Orientation="Horizontal">

                <Rectangle Fill="Chocolate" Width="{Binding Height}" ></Rectangle>

                <TextBlock Text="{Binding Height}"></TextBlock>

            </StackPanel>

        </DataTemplate>

       

 

    </Window.Resources>

    <Grid>

        <DataGrid x:Name="dg" AutoGenerateColumns="False">

            <DataGrid.Columns>

                <DataGridTemplateColumn Header="姓名"  Width="100" CellTemplate="{StaticResource xName}"/>

                <DataGridTemplateColumn Header="年龄"  Width="100" CellTemplate="{StaticResource xAge}"/>

                <DataGridTemplateColumn Header="身高"  Width="250"  CellTemplate="{StaticResource xHeight}"/>

            </DataGrid.Columns>

        </DataGrid>

    </Grid>

</Window>

 

 

 

WPF模板1同样的效果,但是样式已经被存储在资源中,然后被引用。

 

 

 

使用文件存储资源:

WPF模板1

 

 

Test.xaml文件内容

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:local="clr-namespace:WpfApp1">

 

 

    <DataTemplate x:Key="xName" >

        <StackPanel Orientation="Horizontal">

            <TextBlock Text="{Binding Name}" Width="100" Height="25" FontSize="20"

                                           Foreground="white" Background="#939430"

                                           VerticalAlignment="Center" TextAlignment="Left" />

        </StackPanel>

    </DataTemplate>

 

    <DataTemplate x:Key="xAge">

        <StackPanel Orientation="Horizontal">

            <Rectangle Fill="red" Width="{Binding Age}" ></Rectangle>

            <TextBlock Text="{Binding Age}"></TextBlock>

        </StackPanel>

    </DataTemplate>

 

    <DataTemplate x:Key="xHeight">

        <StackPanel Orientation="Horizontal">

            <Rectangle Fill="Chocolate" Width="{Binding Height}" ></Rectangle>

            <TextBlock Text="{Binding Height}"></TextBlock>

        </StackPanel>

    </DataTemplate>

   

</ResourceDictionary>

 

引用资源文件Source属性:当使用Source之后,资源中不能继续编辑其他资源。

<Window x:Class="WpfApp1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp1"

        xmlns:test="clr-namespace:System.Data;assembly=System.Data"

        xmlns:sys="clr-namespace:System;assembly=mscorlib"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>

        <ResourceDictionary Source="Test.xaml">

        </ResourceDictionary>

    </Window.Resources>

    <Grid>

        <DataGrid x:Name="dg" AutoGenerateColumns="False">

            <DataGrid.Columns>

                <DataGridTemplateColumn Header="姓名"  Width="100" CellTemplate="{StaticResource xName}"/>

                <DataGridTemplateColumn Header="年龄"  Width="100" CellTemplate="{StaticResource xAge}"/>

                <DataGridTemplateColumn Header="身高"  Width="250"  CellTemplate="{StaticResource xHeight}"/>

            </DataGrid.Columns>

        </DataGrid>

    </Grid>

</Window>

 

 

WPF模板1

 

仍然是原来的效果,但是存储位置又变化了。

 

 

 

 

 

动态换肤:

Style1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:local="clr-namespace:WpfApp1">

 

 

    <DataTemplate x:Key="xName" >

        <StackPanel Orientation="Horizontal">

            <TextBlock Text="{Binding Name}" Width="100" Height="25" FontSize="20"

                                           Foreground="white" Background="#939430"

                                           VerticalAlignment="Center" TextAlignment="Left" />

        </StackPanel>

    </DataTemplate>

 

    <DataTemplate x:Key="xAge">

        <StackPanel Orientation="Horizontal">

            <Rectangle Fill="red" Width="{Binding Age}" ></Rectangle>

            <TextBlock Text="{Binding Age}"></TextBlock>

        </StackPanel>

    </DataTemplate>

 

    <DataTemplate x:Key="xHeight">

        <StackPanel Orientation="Horizontal">

            <Rectangle Fill="Chocolate" Width="{Binding Height}" ></Rectangle>

            <TextBlock Text="{Binding Height}"></TextBlock>

        </StackPanel>

    </DataTemplate>

 

    <Style x:Key="bg" TargetType="{x:Type StackPanel}">

        <Setter Property="Background" Value="red"></Setter>

    </Style>

   

</ResourceDictionary>

 

Style2.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:local="clr-namespace:WpfApp1">

 

 

    <DataTemplate x:Key="xName" >

        <StackPanel Orientation="Horizontal">

            <TextBlock Text="{Binding Name}" Width="100" Height="25" FontSize="20"

                                           Foreground="white" Background="Cyan"

                                           VerticalAlignment="Center" TextAlignment="Left" />

        </StackPanel>

    </DataTemplate>

 

    <DataTemplate x:Key="xAge">

        <StackPanel Orientation="Horizontal">

            <Rectangle Fill="Blue" Width="{Binding Age}" ></Rectangle>

            <TextBlock Text="{Binding Age}"></TextBlock>

        </StackPanel>

    </DataTemplate>

 

    <DataTemplate x:Key="xHeight">

        <StackPanel Orientation="Horizontal">

            <Rectangle Fill="Green" Width="{Binding Height}" ></Rectangle>

            <TextBlock Text="{Binding Height}"></TextBlock>

        </StackPanel>

    </DataTemplate>

 

    <Style x:Key="bg" TargetType="{x:Type StackPanel}">

        <Setter Property="Background" Value="green"></Setter>

    </Style>

   

</ResourceDictionary>

 

MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp1"

        xmlns:test="clr-namespace:System.Data;assembly=System.Data"

        xmlns:sys="clr-namespace:System;assembly=mscorlib"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>

        <ResourceDictionary Source="style1.xaml"></ResourceDictionary>

    </Window.Resources>

    <Grid>

        <StackPanel Style="{DynamicResource bg}">

            <DataGrid x:Name="dg" AutoGenerateColumns="False" Height="300">

                <DataGrid.Columns>

                    <DataGridTemplateColumn Header="姓名"  Width="100" CellTemplate="{StaticResource xName}"/>

                    <DataGridTemplateColumn Header="年龄"  Width="100" CellTemplate="{StaticResource xAge}"/>

                    <DataGridTemplateColumn Header="身高"  Width="250"  CellTemplate="{StaticResource xHeight}"/>

                </DataGrid.Columns>

            </DataGrid>

            <Button Content="换肤" Width="100" Height="20" Click="Button_Click"></Button>

        </StackPanel>

    </Grid>

</Window>

 

 

MainWindow.cs

 

public partial class MainWindow : Window

    {

        public string str = "1";

        public MainWindow()

        {

 

          

 

            InitializeComponent();

 

 

          

 

            List<Student> students = new List<Student>();

            students.Add(new Student() { Name = "王伟", Age = 13, Height = 167 });

            students.Add(new Student() { Name = "赵明明", Age = 12, Height = 177 });

            students.Add(new Student() { Name = "张三", Age = 20, Height = 174 });

            students.Add(new Student() { Name = "里斯", Age = 22, Height = 180 });

 

            dg.ItemsSource = students;

        }

 

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            this.Resources.Clear();

            str = (int.Parse(str) + 1).ToString();

            if (str =="3")

            {

                str = "1";

            }

 

            ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(new Uri("style"+ str + ".xaml", UriKind.Relative));

            this.Resources.MergedDictionaries.Add(resourceDictionary);

        }

    }

 

    public class Student

    {

        public string Name { get; set; }

        public int Height { get; set; }

        public int Age { get; set; }

 

}

 

 

WPF模板1

WPF模板1

 

可以实现动态换肤了,但是只能对style,却切换不良celltemplate的样式,

因为celltemplate只能是静态资源,style可以是静态资源,虽然切换了资源文件,

但是静态资源只加载一次,因此,只有style达到了在程序运行时进行更换皮肤的效果。