WPF 动态更换模板详解编程语言
2023-06-13 09:20:38 时间
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:模板选择器"
Title="MainWindow" Height="350" Width="525"
Window.Resources
local:MyDataTemplateSelector x:Key="tmpdog" /local:MyDataTemplateSelector
DataTemplate x:Key="dta"
Border BorderThickness="2" BorderBrush="red" Background="GreenYellow"
StackPanel Orientation="Horizontal"
TextBlock Text="{Binding Name}" Margin="10" /TextBlock
TextBlock Text="{Binding ID}" Margin="10" /TextBlock
/StackPanel
/Border
/DataTemplate
DataTemplate x:Key="dtb" DataType="ListBox"
Border BorderThickness="1" BorderBrush="Blue" Background="LightBlue" Padding="5"
StackPanel Orientation="Horizontal"
TextBlock Text="{Binding Name}" Margin="10" /TextBlock
TextBlock Text="{Binding ID}" Margin="10" /TextBlock
/StackPanel
/Border
/DataTemplate
/Window.Resources
Grid
Grid.RowDefinitions
RowDefinition Height="*" /RowDefinition
RowDefinition Height="Auto" /RowDefinition
/Grid.RowDefinitions
ListBox Name="listbox" ItemTemplateSelector="{StaticResource tmpdog}"
/ListBox
Button Grid.Row="1" Width="75" Height="23" Click="Button_Click" 确定 /Button
/Grid
/Window
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace 模板选择器 /// summary /// MainWindow.xaml 的交互逻辑 /// /summary public partial class MainWindow : Window public MainWindow() InitializeComponent(); InitData(); listbox.ItemsSource = list; public List Student list { get; set; } public void InitData() Random r = new Random(); list = new List Student for (int i = 0; i i++) list.Add(new Student Name = "dog" + i, ID = r.Next(100) }); int i = 0; private void Button_Click(object sender, RoutedEventArgs e) if (i%2==0) listbox.SetResourceReference(ListBox.ItemTemplateProperty, "dta"); else listbox.SetResourceReference(ListBox.ItemTemplateProperty, "dtb"); i++; public class Student public int ID set; get; public string Name set; get; public class MyDataTemplateSelector : DataTemplateSelector public override DataTemplate SelectTemplate(object item, DependencyObject container) var fe = container as FrameworkElement; var obj = item as Student; DataTemplate dt = null; if (obj != null fe != null) if (obj.ID 30) dt = fe.FindResource("dta") as DataTemplate; else dt = fe.FindResource("dtb") as DataTemplate; return dt;
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/13170.html
cjavawindowsxml相关文章
- WPF MVVM 模式下自写自用的窗口样式
- WPF 第三方控件学习使用——可停靠布局控件(AvalonDock)
- WPF实现选项卡效果(1)——使用AvalonDock
- WPF项目从.Net Framework迁移到.Net6
- wpf的GridSplitter使用
- C# WPF:把文件给我拖进来!!!
- WPF ComboBox 使用 ResourceBinding 动态绑定资源键并支持语言切换
- OxyPlot 导出图片及 WPF 元素导出为图片的方法
- 分享一个WPF 实现 Windows 软件快捷小工具
- WPF效果第二百零九篇之下载加密的m3u8
- WPF客户端,Python底层,公开招标信息订阅系统
- WPF 性能测试
- WPF连接MySQL:实现步骤深度剖析(wpf 连接mysql)
- 深入分析WPF客户端读取高清图片卡以及缩略图的解决方法详解