简体   繁体   中英

WPF Text Binding to non-static property in singleton class?

I have a preloader screen user control with dynamic text that is bound to a PreloaderContent property within a singleton class. Singleton, because I want to just have a single instance of this property and change it easily within my application. The class is a singleton so I could easily implement INotifyPropertyChanged into the class to update the UI when the property value changes.

This method of binding below reflects the initial property value. However, whenever I change the property via accessing the singleton instance, the change is not reflected..

                <TextBlock Panel.ZIndex="100" Margin="0" TextWrapping="Wrap" Text="{Binding PreloaderContent, Source={x:Static models:Loader.LoaderManager}}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="21" FontWeight="Bold" Foreground="#FFF">

Preloader Singleton

{
    //TO-DO: Make this a singleton class to implement iNotifyPropertyChanged
    //TO-DO: Also, potentially move this into a different directory?
    public class Loader : INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private static Loader _LoaderManager = new Loader();
        public static Loader LoaderManager
        {
            get { return _LoaderManager; }
        }
        // Visbility parameter to determine visbility of custom preloader user control
        private Visibility _preloader;
        public Visibility Preloader
        {
            get
            {
                return _preloader;
            }
            set
            {
                _preloader = value;
                if (_preloader != value)
                {
                    _preloader = value;
                    NotifyPropertyChanged();
                }
            }
        }

        // Textual content showing preloader message inside preloader user control
        private string _preloaderContent;
        public string PreloaderContent {
            get
            {
                return _preloaderContent;
            }
            set
            {
                _preloaderContent = value;
                if (_preloaderContent != value)
                {
                    _preloaderContent = value;
                    NotifyPropertyChanged();
                }
            }
        }

    }
}

XAML Code

Now, I want to bind the Text="" to the property of PreloaderContent (Which exists in another class, not the viewmodel), but I am having issues getting it to actually reflect the changes in the UI when the value changes.

<Grid>
    <Border Panel.ZIndex="1000" d:IsHidden="True" Background="#80000000" Margin="0,0,0,-0.4">
        <Grid>
            <TextBlock Panel.ZIndex="100" Margin="0" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="21" FontWeight="Bold" Foreground="#FFF">
            </TextBlock>
            <TextBlock Panel.ZIndex="100" Margin="11,136,12,75.2" TextWrapping="Wrap" Text="Please Wait..." HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14" FontWeight="Bold" Foreground="#FFF"/>
        </Grid>
    </Border>

另一个类也必须是静态的,或者您的Loader类必须具有另一个类可以设置的属性,该属性将反映在文本框中。

Rookie mistake. I was overriding the preloader value in my Content setter

  _preloader = value;

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