【发布时间】:2020-05-15 09:00:51
【问题描述】:
我有一个 WPF 数据网格和一个名为“请求的日期输出源”的数据模板列。 应用程序应查看单元格中的日期并根据它是今天、过去还是未来来更改颜色。
我有 Ivalue 转换器:
public class FBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string input = value as string;
{
if (input == "Select a date")
{
return new SolidColorBrush(Colors.Black);
}
else
{
DateTime dt = System.Convert.ToDateTime(input);
switch (true)
{
case true when (dt == DateTime.Today):
return new SolidColorBrush(Colors.Yellow);
case true when (dt < DateTime.Today):
return new SolidColorBrush(Colors.Red);
case true when (dt > DateTime.Today):
return new SolidColorBrush(Colors.Blue);
default:
//return Brushes.Black;
return new SolidColorBrush(Colors.Black);
}
}
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
我有 XAML:
<DataGridTemplateColumn x:Name="DateoutSource" Header="Requested 
 Date Out Source" Width="125" SortMemberPath="DateOutSource" SortDirection="Ascending" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker x:Name="BtnDateOutSource" SelectedDate="{Binding DateOutSource}" SelectedDateChanged="BtnDateOutSource_SelectedDateChanged" Foreground="{Binding Converter={StaticResource FBrushConverter}}">
</DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
结果是该列始终为红色:
跟踪代码表明 Convert Object 值始终为 NULL,这就是显示始终为红色的原因。
这意味着绑定存在一些问题。我尝试了各种方法,但都没有成功。
关于这个有什么想法吗? 提前致谢, 克里
【问题讨论】:
-
你在调试转换器的时候检查过
value的实际类型吗?前台绑定没有任何属性路径,因此value肯定是行项目对象,而不是字符串。 -
跟踪产生:System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”上找不到“ForeGround”属性“SourceInspection_30F8E31406EADA6CF79EEC6BB6AC8AE92DC21D809F1D3329238470B5A1906414”(HashCode=38)绑定表达式:路径=前景; DataItem='SourceInspection_30F8E31406EADA6CF79EEC6BB6AC8AE92DC21D809F1D3329238470B5A1906414' (HashCode=38811664);目标元素是'DatePicker'(名称='BtnDateOutSource');目标属性是'Foreground'(类型'Brush')值为null,没有类型。类型为System.Windows.Media.Brush,参数为null。
-
在您的示例中不需要任何名称,您还可以在将 SelectedDate 绑定到属性时处理更改的日期。另外你如何填充
DataGrid?
标签: c# wpf xaml datagrid datatemplate