简体   繁体   中英

WPF Property Binding of TextBlock

I have a popup window, in which I have a texblock which I would like to bind to a property in my ViewModel. I have already successfully binded a boolean in my popup window, and I have basically done the same for my string, but somehow the string property doesn't update...

Here is my .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>

[EDIT]: The changing of the properties takes place when clicking on these buttons:

      <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}"/>

The Property I have successfully binded is OpenPopup in IsOpen="{Binding OpenPopup}", the one that doesn't work is DownloadText in Text="{Binding DownloadText}". The .xaml has a ViewModel which is already wired (as I said, it works fine with all other properties).

The c# code in my ViewModel is: [Edit: Both properties are in the same ViewModel] For the Text-String:

    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();

For the 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;
    }

The RaisePropertyChanged() Method is implemented like this:

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);
}
    }

When the ClosePopup() method is called, the property changes which causes the IsOpen-Property of my Popup Window to change to false and it closes.. just as it should.

When the DownloadVersion() method is called, the property DownloadText is also successfully changed, but not updated in my view. Any suggestions what I am missing?

[EDIT]: The Binding of the buttons:

    public ICommand DownloadVersionCommand {
        get; set;
    }

// In the ViewModel Constructor:
        DownloadVersionCommand = new RelayCommand(o => {
            Debug.Write("DownloadVersionCommand " + o);
            DownloadVersion();
        })

You could try to call the VersionManager.downloadFile() on a background thread:

private void DownloadVersion() {
    DownloadText = "Download gestartet";
    Task.Run(() => VersionManager.downloadFile());
}

Or temporarily just comment out or remove the download call for testing purposes:

private void DownloadVersion() {
    DownloadText = "Download gestartet";
}

Then it should work.

You can't both update the TextBlock and download a file on the same thread simultaneously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM