首页 > 其他分享 >WPF DatagridRow style with ControlTemplate

WPF DatagridRow style with ControlTemplate

时间:2024-12-27 22:29:58浏览次数:7  
标签:style get Windows System ControlTemplate imgs using WPF public

<Window x:Class="WpfApp100.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:WpfApp100"
        WindowState="Maximized"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}" x:Key="tbkStyle">
            <Setter Property="FontSize" Value="50"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="TextWrapping" Value="Wrap"/>
            <Setter Property="Width" Value="300"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                    <Setter Property="FontSize" Value="100"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="CustomRowStyle" TargetType="DataGridRow">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridRow}">
                        <Border BorderBrush="Blue"
                                BorderThickness="5"
                                Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"
                                Height="{Binding ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Id}" Style="{StaticResource tbkStyle}"/>
                                <TextBlock Text="{Binding ISBN}" Style="{StaticResource tbkStyle}"/>
                                <Image Source="{Binding ImgUrl}"
                                       Stretch="Uniform"
                                       RenderOptions.BitmapScalingMode="Fant"/>
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="AlternationIndex" Value="1">
                    <Setter Property="Background" Value="LightBlue"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  RowStyle="{StaticResource CustomRowStyle}"
                  VirtualizingPanel.IsContainerVirtualizable="True"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.CacheLength="1"
                  VirtualizingPanel.CacheLengthUnit="Item"/>
    </Grid>
</Window>


//cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
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 WpfApp100
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new BookVM();
            this.DataContext = vm;
        }
    }

    public class BookVM : INotifyPropertyChanged
    {
        public BookVM()
        {
            InitData();
        }

        private void InitData()
        {
            var imgs = Directory.GetFiles(@"../../Images");
            if(imgs!=null && imgs.Any())
            {
                int imgsCount = imgs.Count();
                BooksCollection = new ObservableCollection<Book>();
                for(int i=0;i<imgsCount;i++)
                {
                    BooksCollection.Add(new Book()
                    {
                        Id = i + 1,
                        ISBN = $"ISBN_{Guid.NewGuid().ToString("N")}",
                        ImgUrl = $"{imgs[i % imgsCount]}"
                    });
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection ?? (new ObservableCollection<Book>());
            }
            set
            {
                if(value!=booksCollection)
                {
                    booksCollection = value;
                    OnPropertyChanged();
                }
            }
        }
    }

    public class Book
    {
        public int Id { get; set; }

        public string ISBN { get; set; }

        public string ImgUrl { get; set; }
    }
}

 

 

 

 

 

 

 

 

 

 

 

标签:style,get,Windows,System,ControlTemplate,imgs,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18636845

相关文章

  • 【WPF】WPF 双向绑定中的 SelectedItem 与 ViewModel 属性更新机制详解
     在WPF开发中,ListBox等控件常用于显示绑定的数据集合,其中ItemsSource绑定的数据源,在没有显式设置 Mode 属性时,默认为单向绑定,它将数据源集合的内容传递给 ListBox,但不会反向更新数据源。而SelectedItem ,默认情况下它的绑定是双向的。这意味着当用户在 ListBox 中......
  • WPF Combobox屏蔽按上下键切换选项
    最近在项目中有一个需求是Combobox可以进行编辑,类似于多行文本框一样进行编辑,在编辑的过程中可能需要上下键切换当前的行,但是combobox的上下键对应了切换选项,所以需要屏蔽combobox的上下键切换功能,并且加上文本框换行的功能combobox自身的previewkeydown事件是无法捕获到上下键的......
  • StyleShot任意风格快照
    ✨✨欢迎大家来访Srlua的博文(づ ̄3 ̄)づ╭❤~✨✨......
  • 解决WPF弹出子窗体如何设置停靠在主窗体的边缘
    窗体代码思路获取主窗体的位置坐标,根据主窗体的位置坐标和长宽尺寸计算子窗体的实际位置,并赋值给子窗体的Top和Left属性。publicpartialclassPromptDialogBox:Window{///<summary>///关闭计时器///</summary>privateDispatcherTimer_timerCl......
  • C# WPF PrintDialog 打印(3)
    前面https://www.cnblogs.com/yinyu5/p/18634080使用PrintDocument方法打印了Canvas,这里打印下面的DataGrid列表内容:这里DataGrid的数据源是DataTable,后台代码:1privatevoidPrintDocument_DataTable_Method(stringTitle,DataTabledataTable)2{3......
  • C# WPF PrintDialog 打印(2)
    前面https://www.cnblogs.com/yinyu5/p/18633910使用PrintVisual方法只打印了可见部分的元素,所以这里改为使用PrintDocument方法来进行打印。需要引用System.Printing.dll界面代码:1<Windowx:Class="WpfApp123.MainWindow"2xmlns="http://schemas.microsoft.com......
  • C# WPF PrintDialog 打印(1)
    参考“WPF打印实例”的文章:https://www.cnblogs.com/gnielee/archive/2010/07/02/wpf-print-sample.html测试程序: 首先打印Canvas效果: 看起来似乎没问题,但是调整窗体尺寸遮挡部分元素:再打印Canvas效果: 可以发现PrintVisual方法只打印了可见部分的元素,测试打印DataG......
  • WPF TabControl 去掉鼠标悬浮效果
     1.资源<Window.Resources><Stylex:Key="TabStyle"TargetType="TabItem"><SetterProperty="TextBlock.FontSize"Value="12"/><SetterProperty="Template">......
  • WPF 构建属性结构
    在WPF中构建树形结构首先构建一个属性结构的类,在这个类中,其中NodeId、NodeName、ParentId是最重要的3个属性,是构建树形结构的关键.另外一个比较重要的属性就是ChildNodes,它是一个树形集合类对象,该属性存储子树,是构建树形结构的必要条件。其他都是附加属性,如Icon用于存储图标......
  • WPF TileMode ViewPort ImageBrush VisualBrush
    <Windowx:Class="WpfApp98.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......