![](/img/trans.png)
[英]Static property of inner static class of non-static class shared between instances of the non-static class?
[英]WPF Text Binding to non-static property in singleton class?
我有一个带有动态文本的preloader屏幕用户控件,该控件绑定到单例类中的PreloaderContent
属性。 Singleton,因为我只想拥有此属性的一个实例,然后在我的应用程序中轻松更改它。 该类是单例,因此我可以轻松地将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();
}
}
}
}
}
现在,我想将Text=""
绑定到PreloaderContent的属性(存在于另一个类中,而不是viewmodel中),但是当值更改时,它实际上无法反映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>
另一个类也必须是静态的,或者您的Loader类必须具有另一个类可以设置的属性,该属性将反映在文本框中。
新秀错误。 我在内容设置器中覆盖了预加载器的值
_preloader = value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.