[英]Binding DataGrid's FontWeight property to value converter
I'm trying to make cell bold or normal if item new/old inside DataGrid but stumbled upon erro.. 我试图使单元格加粗或正常,如果在DataGrid中有新/旧项目,但偶然发现错误。
Looks like my issue described here: Why can I not bind the Visiblity of a DataGridTemplateColumn in Silverlight 4? 看起来像我在这里描述的问题: 为什么我不能在Silverlight 4中绑定DataGridTemplateColumn的可见性?
I'm getting following error: 我收到以下错误:
Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Windows.FontWeight'.
'System.Windows.Data.Binding'类型的对象不能转换为'System.Windows.FontWeight'类型。
And my XAML looks like so: 我的XAML看起来像这样:
<sdk:DataGridTextColumn Header="Subject" Binding="{Binding Subject}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" FontWeight="{Binding IsNew, Converter={StaticResource BoolToFontWeightConverter}}" />
My question is there any workaround to get this working? 我的问题是有什么解决方法可以解决这个问题? I'm not even using template column, it's plain text column..
我什至没有使用模板列,它是纯文本列。
public class BoolToFontWeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((bool)value) ? FontWeights.Bold : FontWeights.Normal;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (FontWeight)value == FontWeights.Bold;
}
}
The FontWeight property of the DataGridTextColumn is not a dependency property and thus will not support bindings. DataGridTextColumn的FontWeight属性不是依赖项属性,因此将不支持绑定。 A workaround would be to use a DataGridTemplateColumn with a TextBox as the DataTemplate and set the appropriate bindings there, something like:
一种解决方法是使用带有TextBox的DataGridTemplateColumn作为DataTemplate并在那里设置适当的绑定,例如:
<sdk:DataGridTemplateColumn Header="Subject"
CanUserReorder="True"
CanUserResize="True"
CanUserSort="True"
Width="Auto">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Subject}" FontWeight="{Binding IsNew, Converter={StaticResource BoolToFontWeightConverter}}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.