WPF DataGrid的LoadingRow事件

  • Post author:
  • Post category:其他


<Window x:Class=”DataGridExam.MainWindow”

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

xmlns:local=”clr-namespace:DataGridExam”

Title=”MainWindow” Height=”350″ Width=”525″>

<Window.Resources>

<local:ImageConverter x:Key=”ImageConverter”></local:ImageConverter>

</Window.Resources>

<Grid>

<DataGrid Name=”gridProducts” AutoGenerateColumns=”False” FrozenColumnCount=”1″

LoadingRow=”gridProducts_LoadingRow_1″

>

<DataGrid.Columns>

<DataGridTextColumn Header=”Product” Width=”175″ Binding=”{Binding Path=ModelName}”></DataGridTextColumn>

<DataGridTextColumn Header=”Price” Binding=”{Binding Path=UnitCost,StringFormat={}{0:C}}”></DataGridTextColumn>

<DataGridTextColumn Header=”ModelNumber” Binding=”{Binding Path=ModelNumber}”></DataGridTextColumn>

<DataGridTextColumn Header=”Description” Width=”400″ Binding=”{Binding Path=Description}”>

<DataGridTextColumn.ElementStyle>

<Style TargetType=”{x:Type TextBlock}”>

<Setter Property=”TextWrapping” Value=”Wrap”></Setter>

</Style>

</DataGridTextColumn.ElementStyle>

</DataGridTextColumn>

<DataGridTextColumn Header=”CategoryID” IsReadOnly=”True” Binding=”{Binding Path=CategoryID}”></DataGridTextColumn>

<DataGridTemplateColumn Header=”Image” IsReadOnly=”True”>

<DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<Image Stretch=”None” Source=”{Binding Path=ProductImage,Converter={StaticResource ImageConverter}}”></Image>

</DataTemplate>

</DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

</DataGrid.Columns>

</DataGrid>

</Grid>

</Window>

using ClassLibrary;

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 DataGridExam

{


/// <summary>

/// Interaction logic for MainWindow.xaml

/// </summary>

public partial class MainWindow : Window

{


public MainWindow()

{


InitializeComponent();

gridProducts.ItemsSource = StoreDB.GetProducts();

}

private void gridProducts_LoadingRow_1(object sender, DataGridRowEventArgs e)

{



Product p = (Product)e.Row.DataContext;


if (p.UnitCost >= 10)

{


e.Row.Background = new SolidColorBrush(Colors.Orange);

}

else

{


e.Row.Background = new SolidColorBrush(Colors.White);

}

}

}

}



版权声明:本文为dxm809原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。