2012-10-26 60 views
1

我在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 
    } 
} 

显示状态值为我的第二列是确定的,但第一个与椭圆总是空的。

转换器永远不会被调用,所以绑定从不绑定。

有人有一个想法/建议?

谢谢。

+0

尝试使椭圆这会为你工作的默认大小。 – JSJ

+0

你可以在你定义包含转换器的StateToBrush资源的地方显示XAML吗? –

回答

0

看起来你没有设置椭圆的大小?

你必须要设置其宽度和高度属性,它显示我相信......

+0

我添加了“宽度=”10“高度=”10“”,椭圆仍然不可见。 –

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 
} 
+0

我在我的转换器的代码中有一个断点,但它永远不会到达那里。所以转换器从来没有被称为...?! –

+0

您似乎错过了Ellipse中的“Path =”以将其绑定到值。 –