theme 文件

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:WpfProjecrt.Hpcontrol">
    <DataTemplate x:Key="HumanMessageDataTemplate">
  <TextBlock Text="hello world"
  Margin="0,0,0,0"
  HorizontalAlignment="Right"
  Foreground="#4d4d4d"
  TextWrapping="Wrap"
  FontSize="16"
  FontFamily="楷体"/>
  </DataTemplate>

  <DataTemplate x:Key="DriverMessageDataTemplate">

  <Grid HorizontalAlignment="Left">
    <Grid Background="#ffffff">
      <local:TestControl></local:TestControl>
    </Grid>
  </Grid>
  </DataTemplate>

</ResourceDictionary>

2, 将theme 文件添加到App.xaml文件

<Application x:Class="WpfProjecrt.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:WpfProjecrt"
  StartupUri="MainWindow.xaml">
  <Application.Resources>
    <ResourceDictionary>

    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/themes/ThemeList.xaml"/>

      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
</Application>

3, 添加listview 的选择器 ListDataTemplateSelector

public class ListDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
      DataTemplate dt= App.Current.Resources["DriverMessageDataTemplate"] as DataTemplate;
      return dt;

      // return App.Current.Resources["DriverMessageDataTemplate"] as DataTemplate;
    }
}

4, 使用 现实的xml文件

<Window x:Class="WpfProjecrt.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:WpfProjecrt"
xmlns:local2="clr-namespace:WpfProjecrt.Hpcontrol"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>

<StackPanel Grid.Row="0">
<Image x:Name="mg" Height="100" Width="100"></Image>
<TextBlock Name="tb" >open web page</TextBlock>
  <ComboBox x:Name="cb" DisplayMemberPath="name" ItemsSource="{Binding mm}"></ComboBox>
</StackPanel>

  <ListView ItemsSource="{Binding mm}" ItemTemplateSelector="{Binding lss}" Grid.Row="1" x:Name="lw" Padding="0,16,0,0">
  </ListView>
</Grid>
</Grid>
</Window>

对应后面的cs 文件

public class Meal
{
  public string name
  {
    set;
    get;
  }
}

public partial class MainWindow : Window
{
  public MainWindow()
  {
    InitializeComponent();
    this.Loaded += MainWindow_Loaded;
    this.DataContext = this;
    // cb.ItemsSource = mcollection;
    //lw.ItemsSource = mcollection;
    //lw.ItemTemplateSelector = ListViewDataTemplateSelector;
}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
  var link = new Hyperlink()
  {
    NavigateUri = new Uri("https://www.baidu.com"),
    Inlines = { new Run() { Text = "baidu" } }
  };
    link.Click += Link_Click;
    tb.Inlines.Add( link );

    for(int i=0; i<100; i++)
    {
      Meal m = new Meal();
      m.name = i.ToString();
      mcollection.Add(m);
    }
    // mg.Source=new ImageSource()
    BitmapImage image = new BitmapImage(new Uri("./imgs/123.jpg", UriKind.Relative));
    mg.Source = image;

  }
  private ObservableCollection<Meal> mcollection = new ObservableCollection<Meal>();
  public ObservableCollection<Meal> mm
  {
    get
    {
      return mcollection;
    }
  }
  public ListDataTemplateSelector lss
  {
    get
    {
      return ls;
    }
  }
  private ListDataTemplateSelector ls = new ListDataTemplateSelector();
  private void Link_Click(object sender, RoutedEventArgs e)
  {
  System.Diagnostics.Process.Start(((Hyperlink)sender).NavigateUri.ToString());
  }

}

总结, xml 文件绑定属性最好用CLR的包装器包装一下,

否则可能包装不上。

wpf 中 theme 的使用 和 listview 模板的使用.的更多相关文章

  1. 在WPF中自定义控件(3) CustomControl (上)

    原文:在WPF中自定义控件(3) CustomControl (上) 在WPF中自定义控件(3) CustomControl (上)                              周银辉 ...

  2. WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid

    WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 故事背景: 需要检索某目录下文件,并列出来,提供选择和其他功能. 第一版需求: 列出文件供选择即可,代码如下 ...

  3. WPF 中获取DataGrid 模板列中控件的对像

    WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...

  4. WPF中的数据模板(DataTemplate)(转)

    原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/30/694388.html WPF中的数据模板(DataTemplate)        ...

  5. WPF中的ControlTemplate(控件模板)(转)

    原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/28/690993.html WPF中的ControlTemplate(控件模板)     ...

  6. WPF中通过代码定义模板

    WPF中可以再XAML中定义模板,也可以通过C#代码定义模板,通过代码可能更清楚的看清其逻辑,而且代码的好处就是可以随时动态的去操作,而在XAML中定义的一般都是静态的. //控件呈现的显示内容1(这 ...

  7. wpf中的数据模板

    wpf中的模板分为数据模板和控件模板,我们可以通过我们自己定制的数据模板来制定自己想要的数据表现形式.例如:时间的显示可以通过图片,也可以通过简单数字表现出来. 例如: (1)先在Demo这个命名空间 ...

  8. WPF中ListBox /ListView如何改变选中条背景颜色

    适用ListBox /ListView WPF中LISTVIEW如何改变选中条背景颜色 https://www.cnblogs.com/sjqq/p/7828119.html

  9. WPF中的数据模板(DataTemplate)

    原文:WPF中的数据模板(DataTemplate) WPF中的数据模板(DataTemplate)                                                   ...

随机推荐

  1. PYTHON找色不变移动

    import cv2 import aircv as ac import numpy as np def wmhd(sjh): bzz0=0 bzz1=0 bzz2=0 xxa=0 yya=0 xxb ...

  2. HTML - form表单操作

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 医疗器械软件产品经理必读的法规及标准-YY/T0664(二)

    上节主要讲了软件开发策划.软件需求分析.软件系统结构设计三个阶段,这节来分析以下几个阶段. 1.软件单元实现 2.软件集成和集成测试 3.软件系统测试 软件开发过程由若干个活动组成,主要包括软件开发策 ...

  4. odoo里的rpc用法

    import odoorpcdb_name = 'test-12'user_name = 'admin'password = 'admin'# Prepare the connection to th ...

  5. Python基础之实现界面和代码分离

    第一步:用QT Designer画一个TreeWidget,存为treeview4.ui,这个处理前面TreeWidget那一节讲过,这里不细讲 treeview4.py # -*- coding: ...

  6. 第十六篇 -- SuperIO学习

    一.SuperIO 这次主要研究SuperIO读取以及控制风扇转速的问题. 参考文章:https://huchanghui123.github.io/Linux/Linux-Superio-CPU-F ...

  7. Django orm 常用查询筛选总结

    本文主要列举一下django orm中的常用查询的筛选方法: 大于.大于等于 小于.小于等于 in like is null / is not null 不等于/不包含于 其他模糊查询 model: ...

  8. Easyui设置easyui-textbox不可编辑

    转载自:https://blog.csdn.net/qq_23113521/article/details/78801689 在easyui里由于easyui-textbox被封装,通过一般的jque ...

  9. js问题记录

    1.aixos请求响应302重定向时无法获取返回数据, 解决方法:在请求头中添加  headers: { 'X-Requested-With': 'XMLHttpRequest' },

  10. shell常识

    1 #!/bin/bash 2 : << ! 3 #使用变量 4 your_name="qinjx" 5 echo $your_name 6 echo ${your_n ...