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

  • 相关阅读:
    201521123062《Java程序设计》第11周学习总结
    201521123062《Java程序设计》第10周学习总结
    201521123062《Java程序设计》第9周学习总结
    201521123062《Java程序设计》第8周学习总结
    201521123062《Java程序设计》第7周学习总结
    201521123062《Java程序设计》第6周学习总结
    201521123062《Java程序设计》第5周学习总结
    MarkDown 例子
    201521123062《Java程序设计》第4周学习总结
    201521123060 《Java程序设计》第11周学习总结
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14152534.html
Copyright © 2020-2023  润新知