WPF DataGrid交替行样式
Posted 欢迎莅临 SUN WU GANG 的园子!!!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF DataGrid交替行样式相关的知识,希望对你有一定的参考价值。
XAML==>
<Window x:Class="QueueSystem.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="950" WindowStyle="None" Background="{x:Null}" AllowsTransparency="True" MouseLeftButtonDown="DragWindow" WindowStartupLocation="CenterScreen" WindowState="Maximized" Loaded="Window_Loaded"> <Window.Resources> <Style TargetType="{x:Type TextBlock }"> <Setter Property="FontSize" Value="55"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontFamily" Value="SimHei"/> <Setter Property="Margin" Value="0,10,0,0"/> <Setter Property="VerticalAlignment" Value="Center"/> </Style> <Style x:Key="DataGridTextColumnCenterSytle" TargetType="{x:Type TextBlock}"> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </Window.Resources> <Grid> <Image Source="Images/logonBg.jpg" Stretch="Fill"/> <StackPanel Margin="10"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBlock x:Name="tbHospital" Grid.Column="0" HorizontalAlignment="Left" /> <TextBlock Name="tBlockTime" FontSize="40" Grid.Column="1" HorizontalAlignment="Right" Foreground="Green"/> </Grid> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> </Grid.RowDefinitions> <DataGrid Grid.Row="0" RowHeaderWidth="0" x:Name="dgvList" AlternationCount="2" AutoGenerateColumns="False" FontSize="50" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" HorizontalScrollBarVisibility="Hidden" EnableRowVirtualization="False" IsEnabled="True" EnableColumnVirtualization="False" VerticalGridLinesBrush="#FFBCC1BC" HorizontalGridLinesBrush="#FFBCC1BC"> <DataGrid.ColumnHeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="FontFamily" Value="SimHei"/> <Setter Property="Foreground" Value="#104E8B"/> <Setter Property="FontSize" Value="50" /> <Setter Property="Height" Value="Auto" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property="HorizontalContentAlignment" Value="Center"/> </Style> </DataGrid.ColumnHeaderStyle> <DataGrid.RowStyle> <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <Trigger Property="ItemsControl.AlternationIndex" Value="0"> <Setter Property="Background" Value="#1874CD" /> </Trigger> <Trigger Property="ItemsControl.AlternationIndex" Value="1"> <Setter Property="Background" Value="#1C86EE" /> </Trigger> </Style.Triggers> <Setter Property="Foreground" Value="Yellow"/> <Setter Property="FontFamily" Value="SimHei"/> </Style> </DataGrid.RowStyle> <DataGrid.Columns> <DataGridTextColumn ElementStyle="{StaticResource DataGridTextColumnCenterSytle}" Header="检查室" Binding="{Binding QUEUENAME, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/> <DataGridTextColumn ElementStyle="{StaticResource DataGridTextColumnCenterSytle}" Header="检查部位" Binding="{Binding PARTOFCHECK, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/> <DataGridTextColumn ElementStyle="{StaticResource DataGridTextColumnCenterSytle}" Header="当前就诊" Binding="{Binding NAME, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/> <DataGridTextColumn ElementStyle="{StaticResource DataGridTextColumnCenterSytle}" Header="准备就诊 " Binding="{Binding NAME2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/> </DataGrid.Columns> </DataGrid> </Grid> </StackPanel> <StackPanel> </StackPanel> <TextBlock x:Name="tbCurrentContent" Margin="20" Text="XX人正在就诊......" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Bottom"/> </Grid> </Window>
CS==>
using System; using System.Collections.Generic; using System.Threading; using System.Windows; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Threading; using QueueSystem.database; using QueueSystem.Helper; namespace QueueSystem { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private DispatcherTimer DispatcherTimer; public MainWindow() { InitializeComponent(); DispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // 当间隔时间过去时发生的事件 DispatcherTimer.Tick += new EventHandler(ShowCurrentTime); DispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1); DispatcherTimer.Start(); } public void ShowCurrentTime(object sender, EventArgs e) { //获得星期 //this.tBlockTime.Text = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn")); //this.tBlockTime.Text += "\\n"; //获得年月日 //this.tBlockTime.Text = DateTime.Now.ToString("yyyy:MM:dd"); //yyyy年MM月dd日 //this.tBlockTime.Text += "\\n"; //获得时分秒 this.tBlockTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss dddd"); this.tbCurrentContent.Text = Socket.SocketMSG; } private void DragWindow(object sender, MouseButtonEventArgs e) { this.DragMove(); } private void Window_Loaded(object sender, RoutedEventArgs e) { InitData(); InitSetting(); } /// <summary> /// 初始化配置 /// </summary> private void InitSetting() { string path = AppDomain.CurrentDomain.BaseDirectory + "\\\\config.xml"; string hospitalName = XmlHelper.Instance.GetAttributeValueByXmlNode(path, "root//hospital", "value"); string hospitalFontSize = XmlHelper.Instance.GetAttributeValueByXmlNode(path, "root//hospital", "fontSize"); string timeFontSize = XmlHelper.Instance.GetAttributeValueByXmlNode(path, "root//time", "fontSize"); string dgFontSize = XmlHelper.Instance.GetAttributeValueByXmlNode(path, "root//dataGrid", "fontSize"); string currentContentF = XmlHelper.Instance.GetAttributeValueByXmlNode(path, "root//currentContent", "fontSize"); //string serverIp = XmlHelper.Instance.GetAttributeValueByXmlNode(path, "root//server", "ip"); // string hospitalFont = XmlHelper.Instance.GetAttributeValueByXmlNode(path, "root//server", "port"); this.tbHospital.Text = hospitalName; this.tbHospital.FontSize = double.Parse(hospitalFontSize); this.tBlockTime.FontSize = double.Parse(timeFontSize); this.dgvList.FontSize = double.Parse(dgFontSize); this.tbCurrentContent.FontSize = double.Parse(currentContentF); } /// <summary> /// 初始化数据 /// </summary> private void InitData() { List<QueueSystem.database.Query> list = QUEUEBUSINESS.GetVRqueueList(string.Empty, string.Empty); this.dgvList.ItemsSource = list; } } } public string GetAttributeValueByXmlNode(string path, string nodeName, string attributeName) { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode node = doc.SelectSingleNode(nodeName); return node.Attributes[attributeName].Value; }
配置文件:
<?xml version="1.0" encoding="utf-8" ?> <root> <hospital value="XX省人民医院" fontSize="50"/> <time fontSize="45"/> <dataGrid fontSize="50"/> <currentContent fontSize="50"/> </root>
运行效果:
动态设置DataGrid样式==》 this.dgvList.Foreground = new SolidColorBrush(color); Style style = new System.Windows.Style(); style.TargetType = typeof(DataGridColumnHeader); //foreground Setter hForeGround = new Setter(); hForeGround.Property = ForegroundProperty; Foreground = new SolidColorBrush(headers); hForeGround.Value = Foreground; style.Setters.Add(hForeGround); //HorizontalContentAlignment Setter align = new Setter(); align.Property = HorizontalContentAlignmentProperty; HorizontalAlignment = HorizontalAlignment.Center; HorizontalContentAlignment = HorizontalAlignment; style.Setters.Add(align);
以上是关于WPF DataGrid交替行样式的主要内容,如果未能解决你的问题,请参考以下文章