2012-10-26 100 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 =」以將其綁定到值。 –