首页 > 其他分享 >wpf datatemplate

wpf datatemplate

时间:2024-03-24 12:33:06浏览次数:28  
标签:Windows xaml System datatemplate new using wpf MainWindow

//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

相关文章

  • wpf add resource dynamically in cs file
    //xaml<Windowx:Class="WpfApp12.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF canvas draw lines via brush
    //xaml<Windowx:Class="WpfApp11.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF中Image控件的绑定
    转载自:https://www.cnblogs.com/seekdream/p/5277237.html背景在我们平时的开发中会经常用到Image控件,通过设置Image控件的Source属性,我们可以加载图片,设置Image的source属性时可以使用相对路径也可以使用绝对路径,一般情况下建议使用绝对路径,类似于下面的形式 1......
  • WPF initialization for opening and unitialization for closing process
    //xaml<Windowx:Class="WpfApp10.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • [转]WPF 使用 Dispatcher 的 InvokeAsync 和 BeginInvoke 的异常处理差别
    一般认为WPF的Dispatcher的InvokeAsync方法是BeginInvoke方法的平替方法和升级版,接近在任何情况下都应该在业务层使用InvokeAsync方法代替BeginInvoke方法。然而在异常的处理上,这两个方法还是有细微的差别的,不能说是坏事,依然可以认为使用InvokeAsync方法代替BeginI......
  • WPF TextBlock根据值显示不同的内容或格式
    当TextBlock绑定的值IsChek=1时显示为“是”;IsCheck=2时显示为“否”,同时设置文字颜色为红色<TextBlockFontWeight="Bold"VerticalAlignment="Center"><TextBlock.Style><StyleTargetType="TextBlock"><Style.Tr......
  • 分享一个自己写的wpf的datagrid分页工具
    <StackPanelHorizontalAlignment="Center"><DataGridx:Name="datagrid1"AutoGenerateColumns="False"CanUserAddRows="False"Height="200"Margin="0,10">......
  • 【WPF应用4】WPF界面对象编辑
    简介WPF(WindowsPresentationFoundation)是.NET框架的一部分,它为开发人员提供了一个用于构建桌面应用程序用户界面的强大平台。WPF界面对象编辑是指在WPF应用程序中创建、设计和修改用户界面元素的过程。这些界面对象不仅包括基本的控件如按钮、文本框和标签,还包括更复杂的......
  • WPF如何给window加阴影效果
    <Stylex:Key="WindowStyle1"TargetType="{x:TypeWindow}"><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType="{x:TypeWind......
  • WPF 添加系统托盘
    提问WPF如何添加系统托盘回答1.引入nugethandycontrolcsxmlns:hc="https://handyorg.github.io/handycontrol"2.窗体添加控件cs<hc:NotifyIconx:Name="NotifyIconContextContent"Text="软件名称"ContextMenu="{StaticResourceContextMenu}&qu......