我想基于使用Wpf的集合动态更改按钮背景颜色?

时间:2016-07-02 07:32:06

标签: c# wpf

学生:

public class Student
{
    private int _studentNo;
    public int StudentNo
    {
        get { return _studentNo; }
        set { _studentNo = value; }
    }

    private Rank _state;
    public Rank State
    {
        get { return _state; }
        set { _state = value; }
    }
}

等级:

public enum Rank
{
    Pass,
    Fail
}

RankBoard:

public class RankBoard
{

    private static ObservableCollection<Student> _studentList;
    public static ObservableCollection<Student> StudentList
    {
        get { return _studentList; }
        set { _studentList = value; }
    }

    public RankBoard()
    {

        LoadDetails();
    }

    private void LoadDetails()
    {
        StudentList = new ObservableCollection<Student>()
        {
            new Student()
            {
                StudentNo=1,
                State=Rank.Pass
            },
            new Student()
            {
                StudentNo=2,
                State=Rank.Fail
            },
            new Student()
            {
                StudentNo=3,
                State=Rank.Pass
            },
        };
    }
}

BackgroundChangedConvertor:

public class BackgroundChange : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var b = value as Button;
        if(b!=null)
        {
            foreach (var x in RankBoard.StudentList)
            {
                if ((System.Convert.ToInt32(b.Content)==x.StudentNo)&&((x.State.ToString()=="Pass")))
                {
                    return new SolidColorBrush(Colors.Green);
                }
                else
                {
                    return new SolidColorBrush(Colors.Red);
                }
            }
        }
        return null;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

MainWindow.xaml

   <Window x:Class="ButtonColorChanged.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ButtonColorChanged"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <local:BackgroundChange x:Key="Color"/>
</Window.Resources>

<Grid>
    <StackPanel VerticalAlignment="Center">
        <Button x:Name="btn1" Content="1" Width="75" Height="25" Background="{Binding Converter={StaticResource Color}}"/>
        <Button x:Name="btn2" Content="2" Width="75" Height="25" Margin="0 20 0 20" Background="{Binding Converter={StaticResource Color}}"/>
        <Button x:Name="btn3" Content="3" Width="75" Height="25" Background="{Binding Converter={StaticResource Color}}"/>
    </StackPanel>
</Grid>
</Window>

我尝试更改按钮背景颜色取决于Collection中的State(Pass或Fail)。但我在BackgroundChange类中运行此progrom对象值throw null值。所以按钮背景颜色不会影响。我该怎么做。?

1 个答案:

答案 0 :(得分:1)

StudentList

RankBoard属性为static,您正在使用非静态构造函数进行初始化。

因此,创建一个static constructor并在那里进行初始化。

static RankBoard()
{
    LoadDetails();
}

private static void LoadDetails()
{ ... }

其次,您不能var b = value as Button;value参数将包含Button's绑定中的有界值。 value无法像在事件处理程序中那样为您提供sender