我有一个弹出窗口,其中我有一个texblock,我想绑定到我的ViewModel中的属性。我已经在弹出窗口中成功绑定了一个布尔值,我基本上已经为我的字符串做了同样的事情,但不知怎的,字符串属性没有更新......
这是我的.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}"/>
我成功绑定的属性是IsOpen =“{Binding OpenPopup}”中的OpenPopup,不起作用的属性是Text =“{Binding DownloadText}”中的DownloadText。 .xaml有一个ViewModel已经连线(正如我所说,它适用于所有其他属性)。
我的ViewModel中的c#代码是:[编辑:两个属性都在同一个ViewModel中] 对于文本字符串:
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();
对于Popup-boolean:
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);
}
}
当调用ClosePopup()方法时,属性会更改,这会导致弹出窗口的IsOpen-Property更改为false并关闭..就像它应该的那样。
调用DownloadVersion()方法时,属性DownloadText也已成功更改,但在我的视图中未更新。有什么建议我缺少什么吗?
[编辑]:按钮的绑定:
public ICommand DownloadVersionCommand {
get; set;
}
// In the ViewModel Constructor:
DownloadVersionCommand = new RelayCommand(o => {
Debug.Write("DownloadVersionCommand " + o);
DownloadVersion();
})
答案 0 :(得分:1)
您可以尝试在后台线程上调用VersionManager.downloadFile()
:
private void DownloadVersion() {
DownloadText = "Download gestartet";
Task.Run(() => VersionManager.downloadFile());
}
或暂时注释或删除下载调用以进行测试:
private void DownloadVersion() {
DownloadText = "Download gestartet";
}
然后它应该工作。
您无法同时更新TextBlock
并在同一个帖子上下载文件。