zoukankan      html  css  js  c++  java
  • DataTemplate 之 ContentTemplate 的使用

    <Window x:Class="WpfApplication1.Window36"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1.Model"
    Title="Window36" Height="350" Width="623">
    <Window.Resources>
    <!--Converter-->
    <local:AutoMarkToLogoPathConverter x:Key="amp"/>
    <local:NameToPhotoPathConverter x:Key="npp"/>
    <!--DataTemplate For DatialView-->
    <DataTemplate x:Key="DatialViewTemplate">
    <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6">
    <StackPanel>
    <Image x:Name="imgPhoto" Width="400" Height="250" Source="{Binding AutoMark,Converter={StaticResource npp}}"></Image>
    <StackPanel Orientation="Horizontal" Margin="5,0">
    <TextBlock Text="Name:" FontSize="20" FontWeight="Bold"></TextBlock>
    <TextBlock FontSize="20" Margin="5,0" Text="{Binding Name}"></TextBlock>
    </StackPanel>
    <StackPanel Orientation="Horizontal" Margin="5,0">
    <TextBlock Text="AutoMark:" FontWeight="Bold"></TextBlock>
    <TextBlock Margin="5,0" Text="{Binding AutoMark}"></TextBlock>
    <TextBlock Text="Year:" FontWeight="Bold">

    </TextBlock>
    <TextBlock Text="{Binding Year}" Margin="5,0">

    </TextBlock>
    <TextBlock Text="Top Speed:" FontWeight="Bold">

    </TextBlock>
    <TextBlock Text="{Binding TopSpeed}" Margin="5,0">

    </TextBlock>
    </StackPanel>
    </StackPanel>
    </Border>
    </DataTemplate>
    <!--Data Template For ItemView-->
    <DataTemplate x:Key="ItemView">
    <Grid Margin="2">
    <StackPanel Orientation="Horizontal">
    <Image x:Name="igLogo" Grid.RowSpan="3" Width="64" Height="64" Source="{Binding Name,Converter={StaticResource amp}}"></Image>
    <StackPanel Margin="5,10">
    <TextBlock Text="{Binding Name}" FontSize="16" FontWeight="Bold"></TextBlock>
    <TextBlock Text="{Binding Year}" FontSize="14"></TextBlock>
    </StackPanel>
    </StackPanel>
    </Grid>
    </DataTemplate>
    </Window.Resources>
    <!--窗体内容-->
    <StackPanel Orientation="Horizontal">
    <UserControl ContentTemplate="{StaticResource DatialViewTemplate}" Content="{Binding Path=SelectedItem,ElementName=lbInfos}"></UserControl>
    <ListBox x:Name="lbInfos" ItemTemplate="{StaticResource ItemView}"></ListBox>
    </StackPanel>
    </Window>

    convertor 转换类

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

    public class AutoMarkToLogoPathConverter:IValueConverter
    {
    /// <summary>
    /// 正向转
    /// </summary>
    /// <param name="value"></param>
    /// <param name="targetType"></param>
    /// <param name="parameter"></param>
    /// <param name="culture"></param>
    /// <returns></returns>
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        return new BitmapImage(new Uri(string.Format(@"Resource/Image/{0}.png",(string)value),UriKind.Relative));
      }
    /// <summary>
    /// 逆向转未用到
    /// </summary>
    /// <param name="value"></param>
    /// <param name="targetType"></param>
    /// <param name="parameter"></param>
    /// <param name="culture"></param>
    /// <returns></returns>
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        throw new NotImplementedException();
      }
    }

    model 

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

    public partial class Window36 : Window
    {
    public Window36()
    {
      InitializeComponent();
      InitialCarList();
    }

    private void InitialCarList()
    {
      List<Car> infos = new List<Car>() {
      new Car(){ AutoMark="Aodi", Name="Aodi", TopSpeed="200", Year="1990"},
      new Car(){ AutoMark="Aodi", Name="Aodi", TopSpeed="250", Year="1998"},
      new Car(){ AutoMark="Aodi", Name="Aodi", TopSpeed="300", Year="2002"},
      new Car(){ AutoMark="Aodi", Name="Aodi", TopSpeed="350", Year="2011"},
      new Car(){ AutoMark="Aodi", Name="Aodi", TopSpeed="500", Year="2020"}
    };
      this.lbInfos.ItemsSource = infos;
    }
    }

  • 相关阅读:
    用Intellij idea创建Maven项目JDK8源码阅读环境问题整理
    咱的Maven项目能用Junit5吗?
    43- 8 mvc知识点
    42-7 linq
    (Ant编程) Iqueryable 类型介绍
    Iqueryable 类型中 的 使用lambda 注意的坑。 (linq to sql)
    (Ant编程) Linq 的select 方法
    Scala模式匹配
    scala正则表达式
    P4336 [SHOI2016]黑暗前的幻想乡
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14152534.html
Copyright © 2011-2022 走看看