我有数据网格(见下文)有一些日期时间的列如何使背景变为红色,其中rooomNumer是2011年1月8日至2011年5月8日的777,代码落后?房间号列与可观察集合绑定
ObservableCollection<RoomsInfoData> _RoomsInfoCollection = new ObservableCollection<RoomsInfoData>();
public RoomsInfo()
public ObservableCollection<RoomsInfoData> RoomsInfoCollection
{
get { return _RoomsInfoCollection; }
}
public class RoomsInfoData
{
public string RoomType { get; set; }
public string RoomNumber { get; set; }
public string RoomStatus { get; set; }
}
HProDataContext db = new HProDataContext();
var _RoomNumber = (from d in db.SelectRooms select d.roomnumber).ToList();
var _RoomType = (from d in db.SelectRooms select d.roomtype).ToList();
var _RoomStatus = (from d in db.SelectRooms select d.status).ToList();
for (int i = 0; i < _RoomNumber.Count; i++)
{
_RoomsInfoCollection.Add(new RoomsInfoData { RoomNumber = _RoomNumber[i], RoomType = _RoomType[i], RoomStatus = _RoomStatus[i] });
}
答案 0 :(得分:2)
假设
我们将在Window / UserControl的Resources集合中指定一个样式......就像这样
<UserControl.Resources> <!-- could be Window.Resources if datagrid lies in a Window -->
<Style x:Key="RoomNumberValidationCellStyle"
TargetType="{x:Type DataGridCell}"> <!-- DataGridCell because we need to apply this style to specific columns -->
<Style.Triggers>
<DataTrigger Binding="{Binding Path=RoomNumber}" Value="777">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
在将所有列设置为this.dataGrid之后,在代码后面的...执行此操作...
foreach(var col in this.dataGrid.Columns)
{
var headerText = (string)col.Header;
if (headerText == "1/8/2011" || headerText.Header == "2/8/2011"
|| headerText.Header == "3/8/2011" || headerText.Header == "4/8/2011"
|| headerText.Header == "5/8/2011")
col.CellStyle = this.FindResource("RoomNumberValidationCellStyle") as Style;
}
如果有帮助,请告诉我。
答案 1 :(得分:0)
<Style x:Key="RedRowStyle" TargetType="{x:Type dg:DataGridRow}">
<Setter Property="Background" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding PropertyName}" Value="777" >
<Setter Property="Background" Value="DarkGray" />
<Setter Property="Foreground" Value="White" />
</DataTrigger>
</Style.Triggers>
</Style>
只需将PropertyName替换为ViewModel中属性为777的属性,并将其应用于正确的行。