简体   繁体   中英

How to change the Foreground color of a button in a DataGrid depending on values from another column? WPF c#

For a project I need to set buttons foreground color depending on values returned from a database.

Fe if the value == 1 the foreground needs to be green.
when the value == 0 it needs to have its original gray color.

I have the following design for my buttons in my wpf DataGrid

<DataGrid.Columns>
    <DataGridTemplateColumn Width="40">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Name="FavoriteButton" ... Foreground="#454545" Click="Button_Click">ButtonTextHere</Button>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

Other columns are automatically generated by the DataGrid's ItemsSource.

I tried to declare a public button in the behind code of my DataGrid but could not reference the buttons name "FavoriteButton"
This is what I was trying

public partial class DataGridMain : UserControl
{
    public Button btn;

    public DataGridMain()
    {
        InitializeComponent();
        btn = FavoriteButton
    }
}

I already have this code to loop through my DataGrid

foreach (System.Data.DataRowView dr in MainGrid.ItemsSource)
{
    if (dr[9].ToString() == "1")
    {
        // here should come the action to actually
        // change the foreground color of the Button.                   
    }
    else
    {
        // Set original color
    }
}

If anyone knows a good way to handle this, I would be thankful if you shared your knowledge!

Simply add a DataTrigger to your Button like this:

<DataGrid.Columns>
            <DataGridTemplateColumn Width="40">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Name="FavoriteButton" Click="FavoriteButton_Click">ButtonTextHere
                            <Button.Style>
                                <Style TargetType="Button">
                                    <Setter Property="Foreground" Value="#454545"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding}" Value="true">
                                            <Setter Property="Foreground" Value="Green"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Button.Style>
                        </Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>

Moreover, it would nice to map your colors to different StaticResource shared to other UI components in order to let you changed it quickly painless if needed.

Bind the foregroundcolor in your XAML to another color property, so you are able to change it in your code:

<Button x:Name="color" Foreground="Green" Visibility="Hidden"/>

put this in your FavoriteButton:

Foreground="{Binding ElementName=color, Path=Foreground, UpdateSourceTrigger=PropertyChanged}"

Now you can change the color like this:

color.Foreground = Brushes.Gray;

I admit, that this is not a professional way, but it should work

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