【问题标题】:WPF Text Binding to non-static property in singleton class?WPF文本绑定到单例类中的非静态属性?
【发布时间】:2017-07-14 15:24:13
【问题描述】:

我有一个带有动态文本的预加载器屏幕用户控件,该控件绑定到单例类中的PreloaderContent 属性。单例,因为我只想拥有此属性的一个实例并在我的应用程序中轻松更改它。该类是一个单例类,因此我可以轻松地在类中实现 INotifyPropertyChanged 以在属性值更改时更新 UI。

下面的这种绑定方法反映了初始属性值。但是,每当我通过访问单例实例更改属性时,更改都不会反映..

                <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">

预加载器单例

{
    //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 代码

现在,我想将Text="" 绑定到 PreloaderContent 的属性(该属性存在于另一个类中,而不是视图模型中),但是当值更改时,我无法让它实际反映 UI 中的更改。

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

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:

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

    【讨论】:

    • 当您说“其他类”时,您指的是与显示用户控件 UI 元素的 View 关联的 ViewModel?
    【解决方案2】:

    菜鸟失误。我在我的内容设置器中覆盖了预加载器值

      _preloader = value;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-29
      • 2015-10-15
      • 2013-08-25
      • 2011-06-11
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多