我在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
}
}
显示状态值的第二列是正常的,但第一列的椭圆始终为空。
永远不会调用转换器,因此绑定永远不会绑定。
有人有想法/建议吗?
谢谢。
答案 0 :(得分:1)
我刚试过重新创建你的问题,但在我的情况下,它运作得很好。请看下面。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<WpfApplication1:StateToBrush x:Key="StateToBrush"/>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Items}" >
<DataGrid.Columns>
<DataGridTemplateColumn CanUserResize="False" Header="StateEllipse">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Ellipse Fill="{Binding Converter={StaticResource StateToBrush}}" Width="10" Height="10" />
<TextBlock FontWeight="Bold" Foreground="Blue" Text="{Binding}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="State" Binding="{Binding}" />
<DataGridTemplateColumn CanUserResize="False" Header="StateEllipse 2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Ellipse Fill="{Binding Converter={StaticResource StateToBrush}}" Width="10" Height="10" />
<TextBlock FontWeight="Bold" Foreground="Green" Text="{Binding}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = this;
InitializeComponent();
Items = new ObservableCollection<int>();
for (int i = 0; i < 100; i++)
{
Items.Add(i);
}
}
public ObservableCollection<int> Items
{
get { return (ObservableCollection<int>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(ObservableCollection<int>), typeof(MainWindow));
}
[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(stateColor);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
答案 1 :(得分:0)
看起来你没有设置椭圆的大小?
你必须设置它的宽度和高度属性,以便显示我相信......