简体   繁体   中英

Control bound to property value doesn't update

I'm having a bit of a problem with WPF property binding. First the code.

C#

public partial class WPFTextBox: UserControl
{
    private bool _bold;
    public bool Bold
    {
        get { return _bold; }
        set
        {
            _bold = value;
            OnPropertyChanged("Bold");
        }
    }

    private bool _selectionChanged;

    public WPFTextBox()
    {
        InitializeComponent();

        DataContext = this;

        Bold = true;  // <--- This works, the checkbox will be checked
        _selectionChanged = false;
     }

     private void txtDetails_SelectionChanged(object sender, RoutedEventArgs e)
     {
         var selection = txtDetails.Selection;
         _selectionChanged = true;
         Bold = selection.FontWeight() == FontWeights.Bold; 
         // ^-- This doesn't work It will trigger everything, but the checkbox won't
         // change value. FontWeight() is an extension I wrote
         _selectionChanged = false;
     }

     private void OnPropertyChanged(string name)
     {
         if(_selectionChanged)
            return; // If the change was brought from the user moving the
                    // cursor in the textbox, don't change the textbox.
         TextRange range = txtDetails.Selection;
         switch(name)
         {
             case "Bold":
                 // change selection to bold, like I mentioned I does work
                 break;
             default:
                 break;
         }
     }
}

XAML

<RichTextBox Name="txtDetails" SelectionChanged="txtDetails_SelectionChanged"/>

<CheckBox Name="chkBold" Content="Bold" IsChecked="{Binding Path=Bold}"/>

I'm creating a textbox with format options. The binding works in the constructor, but not in the selection changed event. I've tried adding a lot of options to the binding such as Mode=TwoWay and different property changed triggers.

The reason I'm using the _selectionChanged bool is because if I don't check for that, if I have a word was different formatting such as he llo and I click on it, it will change the formatting for all of the word to either bold or not. I think maybe it's because I'm handling it in selection changed event, but then I'm not sure where else I could change property value.

See the example from here , you can just grab the INPC part.

set
{
    _bold = value;
     OnPropertyChanged("Bold");
     NotifyPropertyChanged();
}

You need to inherit INotifyPropertyChanged interface

and implement PropertyChangedEventHandler

public class WPFTextBox: UserControl,System.ComponentModel.INotifyPropertyChanged
    {            
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

And call OnPropertyChanged in the setter of your property

1.You can use UpdateSourceTrigger=PropertyChanged event also. 2.Yes binding work on controls from the Extended WPF Toolkit. IsChecked="{xcd:Path=Bold}"

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