简体   繁体   English

WPF对象上的C#数据绑定

[英]wpf c# databinding on object

say I have this control: 说我有这个控制权:

public partial class bloc999 : UserControl
{
 bloc999Data mainBlock = new bloc999Data();

 public bloc999()
 {
  InitializeComponent();
  mainBlock.txtContents = "100";

  base.DataContext = mainBlock;
 }
}

in the xaml: 在xaml中:

    <TextBox Margin="74,116,106,0" Name="txtContents" 
Text="{Binding Path=txtContents, UpdateSourceTrigger=PropertyChanged,Mode = TwoWay}" />
    <TextBox Margin="74,145,106,132" Name="txtContents2" 
Text="{Binding Path=txtContents2, UpdateSourceTrigger=PropertyChanged,Mode = TwoWay}" />

Then I have this class: 然后我有这个课:

public class bloc999Data : INotifyPropertyChanged
    {
     string _txtContents;
     string _txtContents2;

     public event PropertyChangedEventHandler PropertyChanged;

     void NotifyPropertyChanged(string propName)
     {
            if (this.PropertyChanged != null)
                this.PropertyChanged(
                    this, new PropertyChangedEventArgs(propName));
     }

     public string txtContents2
     {
            get
            {
              return this._txtContents2;
            }
            set
            {
                if (int.Parse(value) > int.Parse(this._txtContents))
                {
                    this._txtContents2 = "000";
                }
                else
                    this._txtContents2 = value;

                NotifyPropertyChanged("txtContents2");
            }
        }

        public string txtContents 
        {
            get
            {
                return this._txtContents;
            }
            set 
            {
                this._txtContents = value;
                NotifyPropertyChanged("txtContents");
            } 
        }
    }

Ok now say I have A button on the form and I do this in the code: 好的,现在说我在表单上有一个按钮,并在代码中执行此操作:

mainBlock.txtContents2 = "7777777";

It puts 000 in the textbox, but If i just type in manually, in the textbox (txtContents2), the setter code is called but for some reason the textboxes value does not change, the instance value does change. 它在文本框中输入000,但是如果我只是手动输入文本框(txtContents2),则将调用setter代码,但由于某些原因,文本框的值不会更改,实例值也会更改。 help? 救命?

I believe it's just because the value is changing within the context of the data binding operation, so WPF just ignores it because it knows the value is changing and thinks the event is superfluous. 我相信这仅仅是因为值在数据绑定操作的上下文中正在更改,所以WPF只是忽略了它,因为它知道值正在更改并且认为该事件是多余的。 What it doesn't know is that you've gone and changed the value from the value WPF has to something else again. 知道的是您已经离开并将WPF的值再次更改为其他值。

If you do the notification in a separate message then WPF will process it outside the context of the current data binding operation and will thus pick up the change: 如果您在单独的消息中进行通知,则WPF将在当前数据绑定操作的上下文之外对其进行处理,从而获取更改:

if (int.Parse(value) > int.Parse(this._txtContents))
{
    this._txtContents2 = "000";

    // notify WPF of our change to the property in a separate message
    Dispatcher.BeginInvoke((ThreadStart)delegate
    {
        NotifyPropertyChanged("txtContents2");
    });
}
else
{
    this._txtContents2 = value;
    NotifyPropertyChanged("txtContents2");
}

This assumes your view model has access to the Dispatcher . 假设您的视图模型可以访问Dispatcher An example of how to do so is shown in my blog post on a base ViewModel class. 如何执行此操作的示例在我的博客文章ViewModel基类上显示。

I was having similar problem earlier here 我之前在这里遇到过类似的问题

In your usercontrol, update Binding and set UpdateSourceTrigger to Explicit 在用户控件中,更新Binding并将UpdateSourceTrigger设置为Explicit

<TextBox Margin="74,145,106,132" x:Name="txtContents2" TextChanged="txtContents2_TextChanged"

Text="{Binding Path=txtContents2, UpdateSourceTrigger=Explicit,Mode = TwoWay}" /> Text =“ {Binding Path = txtContents2,UpdateSourceTrigger = Explicit,Mode = TwoWay}” />

then in the TextChanged event handler update the binding manually by validating the input. 然后在TextChanged事件处理程序中,通过验证输入来手动更新绑定。 move validation logic from property txtContent2's setter in bloc999Data in this event handler 在此事件处理程序中将验证逻辑从bloc999Data中的txtContent2属性的setter中移动

    private void txtContents2_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (int.Parse(txtContents2.Text) > int.Parse(mainBlock.txtContents))
        {
            mainBlock.txtContents2 = "000";
            txtContents2.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
        }
        else
        {
            mainBlock.txtContents2 = txtContents2.Text;
            txtContents2.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }
    }

and it works. 而且有效。

Hope it helps!! 希望能帮助到你!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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