【发布时间】:2012-10-26 13:52:27
【问题描述】:
我在 DataGridTemplateColumn 中有一个 Ellipse,它绑定的数据在下一列中正确显示,但我的椭圆列始终为空,没有显示。
<DataGridTemplateColumn CanUserResize="False" Header="StateEllipse">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Ellipse Fill="{Binding Path=State, Converter={StaticResource StateToBrush}}" Width="10" Height="10" />
<TextBlock Text="{Binding Path=State}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="State" Binding="{Binding Path=State}" />
我的转换器看起来像这样:
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
namespace ThisNS.NS.Converter
{
[ValueConversion(typeof(int), typeof(Brush))]
public sealed class StateToBrush : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Color stateColor = Colors.Yellow;
switch ((int)value)
{
case 0:
stateColor = Colors.Green;
break;
case 1:
stateColor = Colors.Red;
break;
}
return new SolidColorBrush(Colors.Yellow);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
我的第二列显示状态值是好的,但第一个带有椭圆的列总是空的。
转换器永远不会被调用,因此绑定永远不会被绑定。
有人有想法/建议吗?
谢谢。
【问题讨论】:
-
尝试设置椭圆的默认大小,这对你有用。
-
您能否展示定义包含转换器的 StateToBrush 资源的 XAML?
标签: c# wpf wpfdatagrid ellipse datagridtemplatecolumn