//xaml <Window x:Class="WpfApp13.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:WpfApp13" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <!--<local:IdColorConverter2 x:Key="idColorConverter"/>--> <DataTemplate x:Key="lbxTemplate" DataType="{x:Type local:Book}" > <Border Name="border" BorderBrush="Blue" BorderThickness="10" Padding="5" Margin="5"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Text="Id:" /> <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Id}"/> <TextBlock Grid.Row="1" Grid.Column="0" Text="ISBN:"/> <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=ISBN}"/> <TextBlock Grid.Row="2" Grid.Column="0" Text="Name"/> <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Name}"/> </Grid> </Border> <DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsMouseOver}" Value="True"> <Setter TargetName="border" Property="Height" Value="200"/> </DataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True"> <Setter Property="Cursor" Value="Hand"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </Window.Resources> <Grid> <ListBox x:Name="lbx" ItemsSource="{Binding}" ItemTemplate="{StaticResource ResourceKey=lbxTemplate}"/> </Grid> </Window> //xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp13 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { TextBlock tb = new TextBlock(); tb.Background = new SolidColorBrush(Colors.Red); InitializeComponent(); InitLbxItemSource(); } private void InitLbxItemSource() { List<Book> booksList = new List<Book>(); for(int i=0;i<10;i++) { booksList.Add(new Book() { Id=i+1, Name=Guid.NewGuid().ToString(), ISBN=Guid.NewGuid().ToString(), Remainder=(i+1)%2 }); } lbx.ItemsSource= booksList; } } } //Book.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp13 { public class Book { public int Id { get; set; } public string ISBN { get; set; } public string Name { get; set; } public int Remainder { get; set; } } }
Mouseover
标签:Windows,xaml,System,datatemplate,new,using,wpf,MainWindow From: https://www.cnblogs.com/Fred1987/p/18092261