DataTrigger is not binding to a global static property? [duplicate]

血红的双手。 提交于 2021-01-29 14:31:15

问题


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).


回答1:


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



回答2:


If you want to access a Global member from XAML you have to follow either of these options:

  1. "{Binding Source={x:Static namespace:StaticClass.StaticMember}}" note that in this situation both class and its member are defined as static.

    1.1. Clemens pointed out that in WPF 4.5 the syntax can be simplified into "{Binding Path=(namespace:StaticClass.StaticMember)}"

  2. Having a static resource defined like: <namespace:Class x:Key="myKey"/> and then use it like: "{Binding Source={StaticResource myKey}, Path=MyProperty}" note that in this situation neither the class nor the property is defined as static.

Your code would be:

public static class Globals
{
    public static bool IsFB = true;
}
<DataTrigger Binding="{Binding Source={x:Static vm:Globals.IsFB}}" Value="True">
    <Setter Property="Background" Value="Red"/>
</DataTrigger>

or

public class Globals
{
    public bool IsFB { get; set; } = true;
}
<Window.Resources>
    <vm:Globals x:Key="myKey"/>
</Window.Resources>
...
<DataTrigger Binding="{Binding Source={StaticResource myKey}, Path=IsFB}" Value="True">
    <Setter Property="Background" Value="Red"/>
</DataTrigger>

If you only need one-time (compile time) binding, then go with the first option otherwise the second option gives you more flexibility.



来源:https://stackoverflow.com/questions/53589290/datatrigger-is-not-binding-to-a-global-static-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!