Search code examples
c#wpfdatatrigger

DataTrigger is not binding to a global static property?


I am using DataTrigger to change the background color in WPF. I have a property in Globals Class which have a true and false value. I check a lot of code from Stackoverflow but I didn't get it working. Please check.

<Grid Height="350" Width="525" Name="gridTesting">
        <Grid.Style>
            <Style TargetType="{x:Type Grid}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=Background, Path=IsFB}" Value="True">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding ElementName=Background, Path=IsFB}" Value="False">
                        <Setter Property="Background" Value="Blue"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
    </Grid>

 public static bool IsFB = true;

I have manually set the variable in my code behind c# file. Any idea why it's failed to run. I change the property and make changes for DataTrigger but not work.

I need to change the background color in this case (on the basis of the value that is selected (compiled time).


Solution

  • You have no binding, because didn't define your DataContext in MainWindow.xaml.cs

    With the following code it's working

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WPFDataTriggers
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            private bool isFB = true;
            public bool IsFB
            {
                get { return isFB; }
                set
                {
                    isFB = value;
                    this.NotifyPropertyChanged("IsFB");
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void NotifyPropertyChanged(string nomPropriete)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(nomPropriete));
            }
    
            private bool NotifyPropertyChanged<T>(ref T variable, T valeur, [CallerMemberName] string nomPropriete = null)
            {
                if (object.Equals(variable, valeur)) return false;
    
                variable = valeur;
                NotifyPropertyChanged(nomPropriete);
                return true;
            }
            public MainWindow()
            {
                InitializeComponent();
                IsFB = true;
                this.DataContext = this;
            }
        }
    }