0

我在数据模板中有一个带有标签的 wpf 数据网格。我想将文本的颜色绑定到一个属性,但它不起作用。

这是xaml。

<DataGrid x:Name="ResultsDataGrid" CanUserSortColumns="True"  MouseDown="ResultsDataGrid_OnMouseDown" SelectionChanged="ResultsDataGrid_OnSelectionChanged" 
IsReadOnly="False" AlternatingRowBackground="WhiteSmoke" CanUserAddRows="False" Margin="10" AutoGenerateColumns="False" VerticalAlignment="Stretch">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Test ID" Width="150" IsReadOnly="True" SortMemberPath="TestDate">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Label Foreground="{Binding PassedColor}"  Content="{Binding TestID}"></Label>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

这里是财产。

public Brush PassedColor
{
    get
    {
        return new SolidBrush(Color.Red);
    }
}

我似乎无法弄清楚我做错了什么。

如果它删除绑定并将前景设置为red它可以工作。所以这绝对是绑定的东西。

编辑:

这是整个对象

public class LabelInfo : INotifyPropertyChanged
{
    private bool _isSelected;
    private double? _karat;
    private bool _passed;
    public string TestID { get; set; }
    public string Label1 { get; set; }
    public string Label2 { get; set; }
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string HasPassed { get { return Passed ? "Yes" : "No"; } }

    public Brush PassColor
    {
        get
        {
            return Brushes.Red;
        }
    }

    public bool Passed
    {
        get { return _passed; }
        set
        {
            _passed = value; 
            NotifyPropertyChanged();
        }
    }

    public bool Final { get; set; }
    public DateTime? TestDate { get; set; }
    public Double RealTime { get; set; }
    public string JTVID { get; set; }
    public int AnalysisID { get; set; }
    public List<ElementResults> Elements { get; set; }
    public double Karat
    {
        get
        {
            if (_karat == null)
                _karat = CalculateKarat();
            return _karat.Value;
        }
        set { _karat = value; }
    }

    public bool PlatingAlert
    {
        get
        {
            return Karat < 7.5;
        }
    }

    public bool IsSelected
    {
        get { return _isSelected; }
        set { _isSelected = value; NotifyPropertyChanged(); }
    }

    public bool PotentialCoating { get; set; }

    private double CalculateKarat()
    {
        if (Elements == null || Elements.Count == 0) return 0;
        return Elements.Where(ex => ex.Name.ToLower().Trim() == "au").Select(ex => ex.Level).FirstOrDefault();
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
4

1 回答 1

3

您正在使用System.Drawing.Brush来自 WinForms 的 ,但您应该使用 WPF System.Windows.Media.Brush。将您的代码更改为:

using System.Windows.Media;

public Brush PassedColor
{
    get { return new SolidColorBrush(Colors.Red); }
}

或者

public Brush PassedColor
{
    get { return Brushes.Red; }
}

也许System.Drawing从引用的程序集中删除。然后你会立即发现所有这些缺陷。

于 2013-10-24T17:51:46.827 回答