我在 HomePage.xaml 中有這個文本塊和影像圖示,
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Class="MyApp.HomePage">
d:DataContext="{d:DesignInstance Type=HomePageViewModel}">
<Grid>
<TextBlock Text="{Binding Username}"></TextBlock>
<Image Source="{Binding ImageSource}" Width="45"></Image>
</Grid>
</Window>
我有這個 HomePageViewModel 類,但這些 Username 和 ImageSource 屬性位于另一個名為 WebAppViewModel 的視圖模型中。
internal class WebAppViewModel
{
private string userName;
public string Username
{
get
{
return userName;
}
set
{
userName = value;
OnPropertyChanged();
}
}
private bool hasSignedIn;
public bool HasSignedIn
{
get
{
return hasSignedIn;
}
set
{
hasSignedIn = value;
OnPropertyChanged();
}
}
public string ImageSource
{
get; set;
}
}
取決于 HasSignedIn 值,需要顯示影像(灰色圖示影像/綠色圖示影像)
此用戶資料來自另一個服務,我從中為這些屬性分配值。
如何在 HomePage.xaml 中系結這些屬性?當 HasSignedIn 值發生變化時需要通知,以便可以顯示正確的彩色影像,如果 HasSignedIn 為假,則 TextBlock 應該是不可見的。
uj5u.com熱心網友回復:
我在 ImageSource 集屬性中使用了 OnPropertyChanged()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461937.html
上一篇:過濾串列時WPF松散系結
