App.xaml
- XML code
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightApplication15.App" xmlns:app="clr-namespace:SilverlightApplication15"> <Application.Resources> <app:DemoComparableConverter x:Key="ComparableConverter"/> </Application.Resources> </Application>
Page1.xaml
- XML code
<UserControl x:Class="SilverlightApplication15.Page1" 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" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <Grid x:Name="LayoutRoot" Background="White"> <sdk:DataGrid AutoGenerateColumns="False" Name="dataGrid1" > <sdk:DataGrid.Columns> <sdk:DataGridTextColumn Binding="{Binding Caption}" Header="Caption" /> <sdk:DataGridTextColumn Binding="{Binding Value}" Header="Value"/> <sdk:DataGridTemplateColumn Header="Value"> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Value}" VerticalAlignment="Center" Style="{Binding Converter={StaticResource ComparableConverter}}" /> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate> </sdk:DataGridTemplateColumn> </sdk:DataGrid.Columns> </sdk:DataGrid> </Grid> </UserControl>
PS:这是在2010 RC中生成的代码,发现 DataGrid 的引用不是来自程序集了,而是来自 xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
Page1.xaml.cs
- C# code
namespace SilverlightApplication15 { public partial class Page1 : UserControl { public Page1() { InitializeComponent(); this.Loaded +=(sender ,e)=> { this.BindControls(); }; } void BindControls() { ObservableCollection<DemoComparableClass> demos = new ObservableCollection<DemoComparableClass>(); demos.Add( new DemoComparableClass("名称A",101) ); demos.Add(new DemoComparableClass("名称B", 51)); demos.Add(new DemoComparableClass("名称C", 201)); this.dataGrid1.ItemsSource = demos; } } }
其他 class
- C# code
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace SilverlightApplication15 { public enum Results { Normal , Above , Below } public class DemoComparableClass { public string Caption { get; set; } public int Value { get; set; } public DemoComparableClass(string c , int v) { this.Caption = c; this.Value = v; } public Results Result { get { if (this.Value > 100 && this.Value < 200) { return Results.Normal; } else if (this.Value >= 200) { return Results.Above; } else { return Results.Below; } } } } public class DemoComparableConverter : System.Windows.Data.IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is DemoComparableClass) { DemoComparableClass v = (DemoComparableClass)value; if (targetType.Equals(typeof(Style))) { Style s = new Style(typeof(TextBlock)); switch (v.Result) { case Results.Normal: s.Setters.Add(new Setter(TextBlock.ForegroundProperty , new SolidColorBrush(Colors.Brown))); break; case Results.Above: s.Setters.Add(new Setter(TextBlock.ForegroundProperty , new SolidColorBrush(Colors.Red))); s.Setters.Add(new Setter(TextBlock.FontWeightProperty, FontWeights.Bold )); break; case Results.Below: s.Setters.Add(new Setter(TextBlock.ForegroundProperty , new SolidColorBrush(Colors.Green))); break; } return s; } else if (targetType.Equals(typeof(Brush))) { switch (v.Result) { case Results.Normal: return new SolidColorBrush(Colors.Brown); case Results.Above: return new SolidColorBrush(Colors.Red); case Results.Below: return new SolidColorBrush(Colors.Green); } } } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }