大家应该都知道Excel中的Conditional Formatting的功能吧。我们可以设置一些条件,当条件满足时修改格子的格式信息。
例如:一个学生成绩表中,我们可以设置当学生成绩低于60分时,将文字颜色改为红色。
如果我们要在我们的应用程序中实现这样的功能,就必须写比较多的代码。但是,在Avalon中集成了对此项功能的支持。下面是一个XAML描述。
<Grid Name="mainGrid" VerticalAlignment="Top" HorizontalAlignment="Left">
<Grid.Resources>
<c:bgConverter x:Key="BGConverter"/>
<DataTemplate x:Key="BookItemTemplate">
<Grid>
<ColumnDefinition Width="250" SharedSizeGroup="BTitle" />
<ColumnDefinition Width="100" SharedSizeGroup="BISBN" />
<ColumnDefinition Width="*" SharedSizeGroup="BNPAGES" />
<TextBlock TextContent="{Binding Path=Title}" Grid.Column="0"
FontWeight="Bold"/>
<TextBlock TextContent="{Binding Path=ISBN}" Grid.Column="1"/>
<Border Grid.Column="2"
Background="{Binding Path=NumPages,
Converter={StaticResource BGConverter}}">
<TextBlock TextContent="{Binding Path=NumPages}"/>
</Border>
</Grid>
</DataTemplate>
<Style x:Key="liStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Width" Value="Auto"/>
</Style>
</Grid.Resources>
<RowDefinition Height="80" />
<RowDefinition Height="Auto" />
<ColumnDefinition Width="250" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ListBox Name="lb" Height="500" Width="400"
HorizontalAlignment="Left"
ItemsSource="{Binding Path=BookTable}"
ItemTemplate ="{StaticResource BookItemTemplate}"
ItemContainerStyle="{StaticResource liStyle}"
IsSynchronizedWithCurrentItem="True"
IsSelectionRequired="True"
Background="Honeydew"
Grid.IsSharedSizeScope="true"
Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
<TextBlock TextContent="{Binding Path=BookTable#.Count}"
Grid.Row="1" Grid.Column="0" />
<Button Click="OnClick" Grid.Row="1" Grid.Column="1">Add Record</Button>
</Grid>
<Grid.Resources>
<c:bgConverter x:Key="BGConverter"/>
<DataTemplate x:Key="BookItemTemplate">
<Grid>
<ColumnDefinition Width="250" SharedSizeGroup="BTitle" />
<ColumnDefinition Width="100" SharedSizeGroup="BISBN" />
<ColumnDefinition Width="*" SharedSizeGroup="BNPAGES" />
<TextBlock TextContent="{Binding Path=Title}" Grid.Column="0"
FontWeight="Bold"/>
<TextBlock TextContent="{Binding Path=ISBN}" Grid.Column="1"/>
<Border Grid.Column="2"
Background="{Binding Path=NumPages,
Converter={StaticResource BGConverter}}">
<TextBlock TextContent="{Binding Path=NumPages}"/>
</Border>
</Grid>
</DataTemplate>
<Style x:Key="liStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Width" Value="Auto"/>
</Style>
</Grid.Resources>
<RowDefinition Height="80" />
<RowDefinition Height="Auto" />
<ColumnDefinition Width="250" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ListBox Name="lb" Height="500" Width="400"
HorizontalAlignment="Left"
ItemsSource="{Binding Path=BookTable}"
ItemTemplate ="{StaticResource BookItemTemplate}"
ItemContainerStyle="{StaticResource liStyle}"
IsSynchronizedWithCurrentItem="True"
IsSelectionRequired="True"
Background="Honeydew"
Grid.IsSharedSizeScope="true"
Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
<TextBlock TextContent="{Binding Path=BookTable#.Count}"
Grid.Row="1" Grid.Column="0" />
<Button Click="OnClick" Grid.Row="1" Grid.Column="1">Add Record</Button>
</Grid>
代码中用黄色标注的部分是实现Conditional Formatting的主要逻辑。我们再来看一下c:bgConverter的实现:
public class bgConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int numValue = (int)value;
if (numValue < 350)
return System.Windows.Media.Brushes.Green;
else
return System.Windows.Media.Brushes.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int numValue = (int)value;
if (numValue < 350)
return System.Windows.Media.Brushes.Green;
else
return System.Windows.Media.Brushes.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
Ok, 我们来看一下程序的运行效果。
如此就实现了“当书的页数多于350页时背景色改为红色”的功能。基本的实现方式就是在进行数据绑定时,提供了挂接IValueConverter的能力。开发人员可以在这里挂接自己的Converter,实现各种Conditional Formatting.
完整的程序可以在Avalon的SDK中找到。WinFS SDK->Simples->"Avalon" Simples->Technology Simples->Data->Data Binding->Binding with Data in an ADO DataSet Sample
或从这里下载