• 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;
    }
    }

  • 相关阅读:
    flash盖住层的解决办法
    编译Chromium
    关于Ubuntu上的版本依赖问题
    GridBagLayout
    使用JList遇到的奇怪问题
    Swing常用整理
    Swing中改变Table的column大小
    SwingUtilities.invokeLater
    git常用命令
    小马过河(bupt 311)
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14152534.html
Copyright © 2020-2023  润新知