wpf 中 theme 的使用 和 listview 模板的使用.
阅读原文时间:2023年07月08日阅读:2

theme 文件


          

  

                   
  

  

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


  
    

    
      

      
    

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




open web page  

     


对应后面的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 mcollection = new ObservableCollection();
  public ObservableCollection 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的包装器包装一下,

否则可能包装不上。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器