依賴屬性的RaisePropertyChanged只調用一次

[英]RaisePropertyChanged of dependent property is called only once


I'm programming a WPF application with VS 2015. In my window i have per example a TextBox and a ToggleButton for switching between FontWeights.Bold and FontWeights.Normal. In my ViewModel i have two properties. One for the IsChecked-property of the ToggleButton and the other for the FontWeight of the TextBox.

我正在使用VS 2015編寫WPF應用程序。在我的窗口中,我有一個TextBox和一個ToggleButton,用於在FontWeights.Bold和FontWeights.Normal之間切換。在我的ViewModel中,我有兩個屬性。一個用於ToggleButton的IsChecked屬性,另一個用於TextBox的FontWeight。

/// <summary>
/// Gets or sets if the button setFontBold is checked.
/// </summary>
private bool? setFontBoldIsChecked = false;
public bool? SetFontBoldIsChecked
{
    get { return setFontBoldIsChecked; }
    set
    {
        setFontBoldIsChecked = value;
        RaisePropertyChanged("SetFontBoldIsChecked");
        RaisePropertyChanged("TextFontWeight");
    }
}

/// <summary>
/// Gets the fontweight depending on SetFontBoldIsChecked.
/// </summary>
public FontWeight TextFontWeight
{
    get { return (setFontBoldIsChecked == true) ? FontWeights.Bold : FontWeights.Normal; }
}

The FontWeight-property of the TextBox is bound like that:

TextBox的FontWeight屬性綁定如下:

<TextBox x:Name="textbox1" FontWeight="{Binding TextFontWeight}"/>

And the IsChecked-property of the ToggleButton is bound to SetFontBoldIsChecked:

並且ToggleButton的IsChecked屬性綁定到SetFontBoldIsChecked:

<ToggleButton x:Name="setFontBold" IsChecked="{Binding SetFontBoldIsChecked}"/>

When i start the application and click the ToggleButton, so IsEnabled is true, the text appears bold. But if i try this one more time, RaisePropertyChanged("TextFontWeight") doesn't call the Getter of TextFontWeight.

當我啟動應用程序並單擊ToggleButton,因此IsEnabled為true時,文本顯示為粗體。但是,如果我再試一次,RaisePropertyChanged(“TextFontWeight”)不會調用TextFontWeight的Getter。

Why does this happen?

為什么會這樣?

Thanks in advance! Patrick

提前致謝!帕特里克

1 个解决方案

#1


0  

Thanks to a colleague i have found it out! In the XAML all attributes have to be set:

感謝一位同事,我發現了它!在XAML中,必須設置所有屬性:

<TextBox x:Name="textForTextProperty" FontWeight="{Binding TextFontWeight, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>

And the properties in the viewmodel have to be changed a little bit:

並且viewmodel中的屬性必須稍微改變一下:

/// <summary>
/// Gets or sets if the button setFontBold is checked.
/// </summary>
private bool? setFontBoldIsChecked = false;
public bool? SetFontBoldIsChecked
{
    get { return setFontBoldIsChecked; }
    set
    {
        setFontBoldIsChecked = value;
        RaisePropertyChanged("SetFontBoldIsChecked");
        RaisePropertyChanged("TextFontWeight");
    }
}

/// <summary>
/// Gets the fontweight depending on SetFontBoldIsChecked.
/// </summary>
private FontWeight textFontWeight;
public FontWeight TextFontWeight
{
    //get { return textFontWeight; }
    get
    {
        textFontWeight = (SetFontBoldIsChecked == true) ? FontWeights.Bold : FontWeights.Normal;
        return textFontWeight;
    }
    set
    {
        textFontWeight = value;
        RaisePropertyChanged("TextFontWeight");
    }
}

Now it works.

現在它有效。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/04/18/725c7c1f4cda2c5bd518a3e8a9f13c00.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com