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

  • 相关阅读:
    ORACLE磁盘空间占满解决
    AlterID.exe解决teamview商业使用
    ORACLE导入数据库
    python快速读取大文件的最后n行
    python解析requests获取到的xml数据
    重新安装python后,原来在虚拟环境里的django项目启动报错:dyld: Library not loaded: @executable_path/../.Python Referenced from: /Users/mac/.virtualenvs/WYGBlog-env/bin/python Reason: image not found
    test
    DevExpress的GridControl拖拽DraopDown后计算HitInfo的RowHandle错误
    使用DataConnectionDialog在运行时设置数据源连接字符串
    详解DevExpress.LookUpEdit控件实现自动搜索定位功能(转)
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14152294.html
Copyright © 2011-2022 走看看