[英]WPF Trigger based on DataGridCell Value Type
What i am trying is我正在尝试的是
<local:class_converter_data_type x:Key="DataTypeConverter"/>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource DataTypeConverter}}" Value="{x:Type sys:DateTime}">
<Setter Property="FontStyle" Value="Italic" />
</DataTrigger>
</Style.Triggers>
</Style>
public class class_converter_data_type : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.GetType() ?? Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
What errors i am making here?我在这里犯了什么错误? Binding format has any errors or in conversion?绑定格式有误或转换中?
You should not do it like this.你不应该这样做。 The DataGridCell
does not directly contain any type information of the underlying data type. DataGridCell
不直接包含基础数据类型的任何类型信息。 The cleanest way is to listen to the DataGrid.AutoGeneratingColumn
event and configure the appearance of the column's content:最简洁的方法是侦听DataGrid.AutoGeneratingColumn
事件并配置列内容的外观:
MainWindow.xaml主窗口.xaml
<DataGrid AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" />
MainWIndow.xaml.cs MainWIndow.xaml.cs
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var dataGrid = sender as DataGrid;
if (e.PropertyType.Equals(typeof(DateTime))
&& e.Column is DataGridTextColumn column)
{
column.FontStyle = FontStyles.Italic;
}
}
In case you are not auto-generating the columns, you can set the font style by setting the DataGridTextColumn.FontStyle
XAML attribute.如果您没有自动生成列,则可以通过设置DataGridTextColumn.FontStyle
XAML 属性来设置字体样式。
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn FontStyle="Italic" />
</DataGrid.Columns>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.