1

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

我显示状态值的第二列没问题,但带有椭圆的第一列始终为空。

转换器永远不会被调用,因此绑定永远不会被绑定。

有人有想法/建议吗?

谢谢你。

4

2 回答 2

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
}
于 2012-10-27T11:03:18.287 回答
0

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

您必须设置它的 Width 和 Height 属性才能显示我相信...

于 2012-10-26T14:19:36.350 回答