C# WPF 静态、动态资源,资源字典,模板

一.静态和动态资源

1.xaml中

<Window.Resources>
    <!--定义Button的样式-->
    <Style TargetType="Button">
        <Setter Property="FontSize" Value="30"/>
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="60"/>
    </Style>
    
    <!--  定义资源还不能确定此资源是动态的还是静态的,使用时才能确定  -->
    <SolidColorBrush x:Key="ButtonBackground" Color="LightBlue"/>
  
</Window.Resources>
<Grid>
    <StackPanel>
        <!--  使用静态资源  StaticResource+资源key  -->
        <Button
         Background="{StaticResource ButtonBackground}"
         Content="静态资源"/>

        <!--  使用动态资源  DynamicResource+资源key  动态使用资源时,程序在运行期间,能更改 
        资源的值,并造成界面重新渲染。  -->
        <Button
         Background="{DynamicResource ButtonBackground}"/>
         Content="动态资源"     

        <Button Click="Button_Click" Content="更改资源的值"/>
    </StackPanel>
</Grid>

2. xaml.cs交互逻辑中

private void Button_Click(object sender, RoutedEventArgs e)
{
    ResourceDictionary resourceDictionary = this.Resources;

    resourceDictionary["ButtonBackground"] = new SolidColorBrush(Colors.Red);
}

运行效果:

二. 资源字典

1.xaml中

 <StackPanel>
     <TextBlock 
         Style="{DynamicResource TextBlockDefault}" 
         Text="演示换资源字典文件"
         HorizontalAlignment="Center"/>

     <Button
         Click="Button_Click"
         Content="更换皮肤"
         Style="{DynamicResource ButtonDefault}"/>
 </StackPanel>

2.Dict文件夹中

DictionaryRed

  <!--  红色背景,注意不同的皮肤的资源Key相同。  -->
  <Style x:Key="ButtonDefault" TargetType="Button">
      <Setter Property="Width" Value="100"/>
      <Setter Property="Height" Value="60"/>
      <Setter Property="Background" Value="Red"/>
      <Setter Property="Foreground" Value="White"/>
  </Style>

  <Style x:Key="TextBlockDefault" TargetType="TextBlock">
      <Setter Property="FontSize" Value="50" />
      <Setter Property="Foreground" Value="Red" />
  </Style>

DictionaryBlue

 <!--  蓝色背景,注意不同的皮肤的资源Key相同。  -->
 <Style x:Key="ButtonDefault" TargetType="Button">
     <Setter Property="Width" Value="100" />
     <Setter Property="Height" Value="60" />
     <Setter Property="Background" Value="Blue" />
     <Setter Property="Foreground" Value="White" />
 </Style>
 
 <Style x:Key="TextBlockDefault" TargetType="TextBlock">
     <Setter Property="FontSize" Value="50" />
     <Setter Property="Foreground" Value="Blue" />
 </Style>

3.App.xaml中

 <Application.Resources>
     <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
             <!--  默认加载蓝色系皮肤  -->
             <ResourceDictionary x:Uid="rd1" Source="pack://application:,,,/Dict/DictionaryBlue.xaml"/>
         </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
 </Application.Resources>

4.xaml.cs交互逻辑中

 private void Button_Click(object sender, RoutedEventArgs e)
 {
     ResourceDictionary rd = Application.Current.Resources;
     string source = rd.MergedDictionaries[0].Source.ToString();
     if(source.Contains("DictionaryBlue.xaml"))
         rd.MergedDictionaries[0].Source=new Uri("pack://application:,,,/Dict/DictionaryRed.xaml");
     else
         rd.MergedDictionaries[0].Source = new Uri("pack://application:,,,/Dict/DictionaryBlue.xaml");
 }

三.模板 

1.控件模板(ControlTemplate)

2.数据模板(DataTemplate)

3.项面板模板(ItemsPanelTemplate)

以下以数据模板中的单元格模板为例:

xaml中

<StackPanel>
    <TextBlock Text="演示单元格模板"/>
    <DataGrid
        AutoGenerateColumns="False"
        IsReadOnly="True"
        ItemsSource="{Binding Students}">

        <!--  给网络设置列  -->
        <DataGrid.Columns>
            <!--  Binding用来给某列绑定值,前提需要给DataGrid设置ItemsSource属性  -->
            <DataGridTextColumn Binding="{Binding Id}" Header="编号"/>
            <DataGridTextColumn Binding="{Binding Name}" Header="姓名"/>
            <DataGridTextColumn Binding="{Binding Age}" Header="年龄" />
            <DataGridTextColumn Binding="{Binding Sex}" Header="性别" />

            <!--  模板列DataGridTemplateColumn  -->
            <DataGridTemplateColumn Header="操作">
                <!--  单元格模板:是一种数据模板  -->
                <DataGridTemplateColumn.CellTemplate>
                    <!--  数据模板:DataTemplate  -->
                    <DataTemplate>
                        <WrapPanel>
                            <Button Content="删除" />
                            <Button Content="编辑" />
                            <Button Content="设置权限" />
                        </WrapPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

交互逻辑中 

  public MainWindow()
  {
      InitializeComponent();
      // 给当前的Window对象设置上下文
      this.DataContext = new ViewModel();
  }

  public class ViewModel
  {
      public List<Student> Students { get; set; }
      public ViewModel()
      {
          Students = new List<Student>() {
          new Student(){Id=1,Name="aaa",Age=18,Sex=true},
          new Student(){Id=2,Name="bbb",Age=19,Sex=false},
          new Student(){Id=3,Name="ccc",Age=20,Sex=true},
      };
      }
  }

  public class Student
  {
      public int Id { get; set; }
      public string Name { get; set; } = string.Empty;  
      public int Age { get; set; }
      public bool Sex { get; set; }
  }

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值