• dataTemplate 使用


    App

    -----------------------------------------------------------------

    <Application x:Class="WPFDemo.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPFDemo"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
    <local:TempConverter x:Key="myTempConverter"/>
    <DataTemplate x:Key="temp1">
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="temp1" Margin="0 0 10 0"/>
    <TextBlock Text="{Binding Name}"/>
    <Label Background="Red">this is template 1</Label>
    </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="temp2">
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="temp2" Margin="0 0 10 0"/>
    <TextBlock Text="{Binding Name}"/>
    <Label Background="Black">this is template 1000</Label>
    </StackPanel>
    </DataTemplate>
    </Application.Resources>
    </Application>

    mainwindow

    ----------------------------------------------------------

    <Window x:Class="WPFDemo.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:WPFDemo"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <StackPanel>
    <ListBox ItemsSource="{Binding pList}" ItemTemplate="{Binding pList,Converter={StaticResource myTempConverter}}" Height="400" x:Name="listBox1" Width="300" />
    </StackPanel>
    </Window>

    viewModel

    -------------------------------------------------------------------------------

    public class MainViewModel
    {
    public MainViewModel()
    {
    _pList = new ObservableCollection<People>();
    _pList.Add(new People { IsTemp1 = false, Name = "zhangsan" });
    _pList.Add(new People { IsTemp1 = true, Name = "zhangsan" });
    _pList.Add(new People { IsTemp1 = false, Name = "lisi" });
    _pList.Add(new People { IsTemp1 = false, Name = "wangwu" });
    }
    private ObservableCollection<People> _pList = null;
    public ObservableCollection<People> pList
    { get
    {
    return _pList;
    }
    }

    }

    model

    ---------------------------------------------------------------------------------

    public class People
    {
    public bool IsTemp1 { get; set; }
    public string Name { get; set; }
    }

    转换类

    --------------------------------------------------------

    public class TempConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    Collection<People> datalist = (Collection<People>)value;

    DataTemplate template = new DataTemplate();

    if (datalist[0].IsTemp1 == true)
    {
    template = App.Current.Resources["temp1"] as DataTemplate;
    }
    else
    {
    template = App.Current.Resources["temp2"] as DataTemplate;
    }
    return template;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
    throw new NotImplementedException();
    }
    }

  • 相关阅读:
    VTK 体绘制讨论_光照&阴影、VTKLODProp3D
    VTK 体绘制讨论_颜色传输函数
    VTK 体绘制讨论_梯度不透明度传输函数
    VTK 体绘制讨论_不透明度传输函数
    VTK 体绘制裁剪_Cripping技术
    VTK 体绘制裁剪_Cropping技术
    VTK 纹理映射体绘制_三维纹理映射
    VTK 纹理映射体绘制_二维纹理映射
    VTK 体绘制_固定点光线投影体绘制与GPU加速光线投影体绘制
    VTK 体绘制_光线投影+最大密度投影+等值面法
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14152294.html
Copyright © 2020-2023  润新知