简体   繁体   中英

I want to change button background color dynamically in based on collection using Wpf?

Student:

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; }
    }
}

Rank:

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>

I tried to change button background color depends upon to State(Pass or Fail) in Collection. but i Run this progrom object value throw null value in BackgroundChange class. so Button background color doesn't affect.? How do i do it.?

StudentList property of RankBoard is static , and you are initializing in non-static constructor.

So, create a static constructor and do initialization there.

static RankBoard()
{
    LoadDetails();
}

private static void LoadDetails()
{ ... }

Secondly, you can't do var b = value as Button; , the value parameter would contain the bounded value in your Button's binding. value won't give you the sender as in an event handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM