zoukankan      html  css  js  c++  java
  • 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();
    }
    }

  • 相关阅读:
    BasKet Note Pads 1.0 颁发
    为OpenOffice 2.4装置3D幻化结果
    Dolphin:KDE 中的文件操持器
    MySQL Administrator:MySQL 数据库经督工具
    gISOMount-ISO 映像文件挂载东西
    Seahorse:让加密更等闲
    Gmail Notifier:又一个 Gmail 邮件通知步调
    EasyTAG-音频文件 Tag 编辑器
    KAlarm:看护提示挨次
    文泉驿点阵宋体 0.8(嬴政)正式发布
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14152294.html
Copyright © 2011-2022 走看看