我在DataGridTemplateColumn中有一个椭圆,它绑定的数据在下一列中正确显示,但我的椭圆列始终为空,没有显示任何内容。WPF无法在DataGridTemplateColumn中显示椭圆
<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
}
}
显示状态值为我的第二列是确定的,但第一个与椭圆总是空的。
转换器永远不会被调用,所以绑定从不绑定。
有人有一个想法/建议?
谢谢。
尝试使椭圆这会为你工作的默认大小。 – JSJ
你可以在你定义包含转换器的StateToBrush资源的地方显示XAML吗? –