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();
    }
    }

  • 相关阅读:
    鼠标移向小图显示大图
    一个简单漂亮的CSS相册代码
    windows 应该关闭服务
    NetBIOS名称
    DOS命令大全(经典收藏)
    大揭露:Win中也有各种不老实的服务
    变量名
    ASP.NET2.0 GridView小技巧汇粹 (转)
    Dfs实战技术
    windows 2003中活动目录支持文件
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14152294.html
Copyright © 2011-2022 走看看