绑定到属性值的控制不会更新

时间:2015-11-04 00:52:02

标签: c# wpf xaml data-binding

我在WPF属性绑定方面遇到了一些问题。首先是代码。

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

我正在创建一个带格式选项的文本框。绑定在构造函数中起作用,但不在选择更改事件中起作用。我尝试在绑定中添加了很多选项,例如Mode=TwoWay和不同的属性更改触发器。

我使用_selectionChanged bool的原因是因为如果我没有检查,如果我有一个单词是不同的格式,例如他 llo 然后我点击它,它会将所有单词的格式更改为粗体或不粗体。我想也许是因为我在选择改变事件中处理它,但后来我不知道我还能在哪里改变财产价值。

3 个答案:

答案 0 :(得分:1)

请参阅here中的示例,您可以抓住INPC部分。

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

答案 1 :(得分:1)

您需要继承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)); } } }

OnPropertyChanged

在你的财产的设定者中拨打app.controller('TwitterInputController', ['twitterServices', function (twitterServices) { this.twitterHandle = null; // added this.pic = ''; // when user submits twitter handle getCandidateMatcPic calls factory function this.getCandidateMatchPic = function () { twitterServices.getMatchWithTwitterHandle(this.twitterHandle) .then(function (pic) { // this.pic = pic // console.log(this.pic) console.log('AHA heres the picUrl:', pic); return this.pic; }); }; }]);

答案 2 :(得分:0)

1.您也可以使用 UpdateSourceTrigger = PropertyChanged 事件。 2.是否对Extended WPF Toolkit中的控件进行绑定。  的器isChecked = “{XCD:路径=粗体}”