在DataGrid的背景色控制中使用的IValueConvert,需要注意的是代码中的参数value是要转化的值所属的实例,而不仅仅是简单的一个值。
publicobject Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
StaticsStudentItem item = value asStaticsStudentItem;
if (item.Amount_absent>4)
{
return"Red";
}
return"White";
}
<dxr:DXRibbonWindow x:Class="AttendanceCheck.Views.Check.CheckNameView"
xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:converter="clr-namespace:AttendanceCheck.Converters"
WindowStartupLocation="CenterScreen"
Name="window"
Title="点名考勤"
Height="600" Width="800">
<Window.Resources>
<converter:StateConverter x:Key="stateonvert"/>
<converter:ColorConvert x:Key="colorconvert"/>
</Window.Resources>
<DockPanel Name="dockPanel">
<StackPanel DockPanel.Dock="Right">
<GroupBox>
<StackPanel>
<StackPanel Orientation="Horizontal">
<RadioButton Content="课前点名" IsChecked="{Binding BeforeClass}"/>
<RadioButton Content="课后点名" IsChecked="{Binding AfterClass}"/>
</StackPanel>
<StackPanel>
<Label Content="课程"/>
<TextBox Name="txt_cname" Text="{Binding Course.Name}" IsEnabled="False"/>
<Label Content="时间"/>
<TextBox Name="txt_time" Text="{Binding Course.Time}"/>
<Label Content="地点"/>
<TextBox Name="txt_site" Text="{Binding Course.Site}"/>
<Label Content="备注"/>
<TextBox Name="txt_check_item_notes" ToolTip="在这里填写备注"/>
</StackPanel>
<StackPanel DataContext="{Binding ElementName=datagrid,Path=CurrentItem}">
<Label Name="label_current" Content="当前"/>
<StackPanel Orientation="Horizontal">
<Label Content="学号: "/>
<Label Name="label_sid_current" Content="{Binding Sid}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="姓名: "/>
<Label Name="label_sname_current" Content="{Binding Sname}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="状态: "/>
<Label Name="label_status_current" Content="{Binding Converter={StaticResource stateonvert}}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</GroupBox>
<GroupBox>
<StackPanel>
<dxe:ButtonEdit ShowText="False" AllowDefaultButton="False" Margin="1">
<dxe:ButtonInfo Name="btn_previous" Content="上一个" Command="{Binding PreviousCommand}" CommandParameter="{Binding ElementName=datagrid}"/>
<dxe:ButtonInfo Name="btn_normal0" Content="出勤" Command="{Binding NormalCommand}" CommandParameter="{Binding ElementName=datagrid}" Visibility="{Binding ElementName=dockPanel,Path=DataContext.BeforeItemsVisibility,Mode=TwoWay}"/>
<dxe:ButtonInfo Name="btn_normal1" Content="正常" Command="{Binding RightCommand}" CommandParameter="{Binding ElementName=datagrid}" Visibility="{Binding ElementName=dockPanel,Path=DataContext.AfterItemsVisibility,Mode=TwoWay}"/>
</dxe:ButtonEdit>
<dxe:ButtonEdit ShowText="False" AllowDefaultButton="False" Margin="1">
<dxe:ButtonInfo Name="btn_late" Content="迟到" Command="{Binding LateCommand}" CommandParameter="{Binding ElementName=datagrid}"/>
<dxe:ButtonInfo Name="btn_reasonable" Content="请假" Command="{Binding ReasonableCommand}" CommandParameter="{Binding ElementName=datagrid}"/>
<dxe:ButtonInfo Name="btn_absent" Content="旷课" Command="{Binding AbsentCommand}" CommandParameter="{Binding ElementName=datagrid}"/>
<dxe:ButtonInfo Name="btn_leaved" Content="早退" Command="{Binding LeavedCommand}" CommandParameter="{Binding ElementName=datagrid}" Visibility="{Binding ElementName=dockPanel,Path=DataContext.AfterItemsVisibility,Mode=TwoWay}"/>
</dxe:ButtonEdit>
<dxe:ButtonEdit ShowText="False" AllowDefaultButton="False" Margin="1">
<dxe:ButtonInfo Name="btn_commit" Content="提交" Command="{Binding CommitCommand}" CommandParameter="{Binding ElementName=window}"/>
<dxe:ButtonInfo Name="btn_cancel" Content="取消" Command="{Binding CancelCommand}" CommandParameter="{Binding ElementName=window}"/>
</dxe:ButtonEdit>
</StackPanel>
</GroupBox>
</StackPanel>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem Content="{Binding StateBarContent}"/>
</StatusBar>
<Grid>
<GroupBox>
<ScrollViewer CanContentScroll="True">
<dxg:GridControl Name="datagrid" AutoGenerateColumns="None" ShowBorder="False" CurrentItem="{Binding CurrentItem,Mode=TwoWay}"
ItemsSource="{Binding Students}" Height="2000">
<dxg:GridControl.View>
<dxg:TableView AutoWidth="True" AllowEditing="True" VerticalScrollbarVisibility="Hidden"/>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn Header="学号">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RowData.Row.Sid}" Background="{Binding RowData.Row.Amount_late,Converter={StaticResource colorconvert}}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn Header="姓名">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RowData.Row.Sname}" Background="{Binding RowData.Row.Amount_late,Converter={StaticResource colorconvert}}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn Header="性别">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RowData.Row.Sgender}" Background="{Binding RowData.Row.Amount_late,Converter={StaticResource colorconvert}}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn Header="班级">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RowData.Row.Sclass}" Background="{Binding RowData.Row.Amount_late,Converter={StaticResource colorconvert}}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn Header="状态">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="{Binding RowData.Row.Amount_late,Converter={StaticResource colorconvert}}">
<StackPanel Orientation="Horizontal" Visibility="{Binding ElementName=dockPanel,Path=DataContext.BeforeItemsVisibility,Mode=TwoWay}">
<RadioButton Content="出勤" IsChecked="{Binding RowData.Row.IsNormal,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding ElementName=dockPanel,Path=DataContext.NormalCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Content="迟到" IsChecked="{Binding RowData.Row.IsLate,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding ElementName=dockPanel,Path=DataContext.LateCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Content="请假" IsChecked="{Binding RowData.Row.IsReasonable,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding ElementName=dockPanel,Path=DataContext.ReasonableCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Content="旷课" IsChecked="{Binding RowData.Row.IsAbsent,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding ElementName=dockPanel,Path=DataContext.AbsentCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</StackPanel>
<StackPanel Orientation="Horizontal" Visibility="{Binding ElementName=dockPanel,Path=DataContext.AfterItemsVisibility,Mode=TwoWay}">
<RadioButton Content="早退" IsChecked="{Binding RowData.Row.IsLeaved,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding ElementName=dockPanel,Path=DataContext.LeavedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Content="正常" IsChecked="{Binding RowData.Row.IsRight,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding ElementName=dockPanel,Path=DataContext.RightCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</StackPanel>
</StackPanel>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
</dxg:GridControl.Columns>
</dxg:GridControl>
</ScrollViewer>
</GroupBox>
</Grid>
</DockPanel>
</dxr:DXRibbonWindow>