如何在运行时使用wpf中的集合更改按钮颜色?

时间:2016-07-05 05:26: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:BindableBase
{

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

        }
    }

    static RankBoard()
    {
        LoadDetails();
    }

    private static 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
            },
        };
    }

    public void ColorChanged(Student Items)
    {
        Student temp= _studentList.Where(i => i.StudentNo == Items.StudentNo).Single();
        StudentList.Remove(temp);
        StudentList.Add(Items);
    }
}

BindableBase:

public class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string PropertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }
}

BackgroundChange:

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.Where(i=>i.StudentNo==System.Convert.ToInt32(b.Content)))
            {
                if ((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 Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0 30 0 0"> 
        <TextBox x:Name="txt1" Width="75" Height="25"/>
        <TextBox x:Name="txt2" Width="75" Height="25" Margin="20 0 20 0"/>
        <Button x:Name="btnadd" Width="75" Height="25" Content="Add" Click="btnadd_Click"/>
    </StackPanel>

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

如果我更改数据集合,我想在运行时更改按钮颜色。 但如果我把动态资源,异常将抛出。那我该怎么办?

1 个答案:

答案 0 :(得分:0)

结合:

<Button x:Name="btn1" Content="1" Width="75" Height="25" Background="{Binding Path=ButtonColor}"/>

你的班级:

public class RankBoard : BindableBase
{
    private Color _buttonColor;
    public Color ButtonColor 
    {
        get
        {
            return _buttonColor;
        }
        set
        {
            _buttonColor = value;
            RaisePropertyChanged("ButtonColor");
        }
    }

    public void RefreshButtonColor(int content)
    {
        foreach (var x in StudentList.Where(i=>i.StudentNo==content))
        {
            if ((x.State.ToString()=="Pass"))
            {
                ButtonColor = Colors.Green;
            }
            else
            {
                ButtonColor = Colors.Red;
            }
        }
    }
}

如果您想根据学生列表更改按钮的颜色,请致电RefreshButtonColor

当我查看您的代码时,您希望根据其内容更改所有按钮的颜色。如果使用属性,则必须为每个按钮定义属性。在RefreshButtonColor中,您必须根据输入选择要更新的属性。这只是如何更改按钮颜色的示例。