我有一個preloader屏幕用戶控件與動態文本綁定到一個單身類中的PreloaderContent
屬性。 Singleton,因爲我只想擁有這個屬性的單個實例並在我的應用程序中輕鬆更改它。該類是一個單例,所以我可以輕鬆地將INotifyPropertyChanged實現到類中,以便在屬性值更改時更新UI。WPF文本綁定到單身類中的非靜態屬性?
下面的綁定方法反映了初始屬性值。但是,每當我改變通過訪問單實例的屬性,這種變化不會反映..
<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(它存在於另一個類中,而不是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>
當您說「其他類」時,您是否引用與顯示用戶控件UI元素的View關聯的ViewModel? – ryan