我有一個彈出窗口,其中我有一個texblock,我想綁定到我的ViewModel屬性。我已經成功綁定一個布爾值,在我的彈出窗口,我已經基本上完成了我的字符串是相同的,但不知何故字符串屬性不更新......WPF屬性綁定的TextBlock
這裏是我的.xaml:
<Popup Margin ="10" HorizontalAlignment="Center" VerticalAlignment="Top" AllowsTransparency="True" IsOpen="{Binding OpenPopup}" Height="150" Width="300">
<Grid Background="#FFFFCCCC">
<TextBlock x:Name="NewVersionText" Margin="10,10,10,10" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="14" Width="230">
Eine neue Version der Applikation ist verfügbar. <LineBreak /> Möchten Sie diese herunterladen?
</TextBlock>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="10,10,10,10" TextWrapping="Wrap" Width="230" Text="{Binding DownloadText}"/>
</Grid>
</Popup>
[編輯]:這些按鈕點擊時的屬性的改變發生:
<Button Content="Ja" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="70" Command="{Binding DownloadVersionCommand}"/>
<Button Content="Später" HorizontalAlignment="Left" Height="20" Margin="75,0,0,0" VerticalAlignment="Top" Width="70" Command="{Binding ClosePopupCommand}"/>
物業我已經成功綁定是OpenPopup在ISOPEN =「{結合OpenPopup}」,即不所述一個工作是Text =「{Binding DownloadText}」中的DownloadText。 .xaml有一個已經連線的ViewModel(正如我所說的,它可以很好地處理所有其他屬性)。
在我的視圖模型C#代碼是:[編輯:兩個性質是在相同的視圖模型] 對於文本字符串:
private string _downloadText;
public string DownloadText {
get {
return _downloadText;
}
set {
_downloadText = value;
Debug.WriteLine("DownloadText = " + value);
RaisePropertyChanged();
}
}
private void DownloadVersion() {
DownloadText = "Download gestartet";
VersionManager.downloadFile();
對於彈出布爾:
private bool _openPopup;
public bool OpenPopup {
get {
return _openPopup;
}
set {
_openPopup = value;
Debug.WriteLine("Open Popup = " + value);
RaisePropertyChanged();
}
}
private void ClosePopoup() {
OpenPopup = false;
}
RaisePropertyChanged()方法是這樣實現的:
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string propertyName = null) {
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Debug.WriteLine("MainWindowViewModel, PropertyChanged: " + propertyName);
}
}
當CloseP調用opup()方法時,屬性發生變化,導致我彈出窗口的IsOpen-Property變爲false,並關閉..就像它應該那樣。
當調用DownloadVersion()方法時,屬性DownloadText也成功更改,但未在我的視圖中更新。任何建議我失蹤?
[編輯]:綁定按鈕:
public ICommand DownloadVersionCommand {
get; set;
}
// In the ViewModel Constructor:
DownloadVersionCommand = new RelayCommand(o => {
Debug.Write("DownloadVersionCommand " + o);
DownloadVersion();
})
當您運行該程序並顯示彈出窗口時,是否在Visual Studio的輸出窗口中看到任何綁定錯誤? –
如果'DownloadText'和'OpenPopup'都在同一個視圖模型中,那麼您的代碼對我來說非常適用。 –
是的,兩者都在相同的viewmodel ...至於埃裏克的問題:我從輸出窗口得到這個:'App.exe'(CLR v4.0.30319:App.exe):加載'C:\ WINDOWS \ Microsoft.Net \裝配\ GAC_MSIL \ System.Numerics.Vectors \ v4.0_4.0.0.0__b03f5f7f11d50a3a \ System.Numerics.Vectors.dll」。找不到或打開PDB文件。 DownloadVersionCommand DownloadText =下載gestartet MainWindowViewModel,PropertyChanged:DownloadText – scalderon