绑定:文本框中未显示的值

时间:2017-04-02 08:42:29

标签: c# wpf xaml data-binding

我创建了一个绑定到viewmodel的属性对象的文本框,如下所示:

<TextBox x:Name="tbAmount" Text="{Binding Receipt.Amount}" Grid.Column="0"/>

到目前为止工作正常。现在文本框保持为空。

将其更改为

<TextBox x:Name="tbAmount" Text="{Binding Receipt.Amount, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}" Grid.Column="0"/>

没有帮助。

控件背后的代码如下所示

using System.Windows.Controls;
using System.Windows.Data;
using ViewModel.Receipts;

namespace tegControls
{

    public partial class Receipt : UserControl
    {
        public Receipt()
        {
            InitializeComponent();
        }

        public vmReceipt vmEr;

        private int id;

        public int Id
        {
            get { return id; }
            set
            {
                id = value;
                vmEr = new vmReceipt(id);
                this.DataContext = vmEr;

            }
        }

    }
}

使用以下行初始化控件(托管应用程序位于vb.net中)

Receipt1 = New tegControls.Receipt()
Receipt1.ID = int_ID

收据的模型类如下所示:

public class Receipt : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyChangedEventArgs.PropertyName));
        System.Diagnostics.Debug.Print("Receipt: {0}", propertyChangedEventArgs.PropertyName);
    }

    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; OnPropertyChanged(new PropertyChangedEventArgs("Id")); }
    }

    public Single Amount
    {
        get { return Amount; }
        set
        {
            Amount = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Amount"));
        }
    }

}

这是viewmodel类,它充当xmal控件的数据文本

using System.Collections.ObjectModel;
using System.ComponentModel;
using Model;


namespace ViewModel.Receipts
{
    public class vmReceipt : Receipt, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyChangedEventArgs.PropertyName));
        }

        public vmReceipt()
        {
            base.PropertyChanged += VmReceipt_PropertyChanged;
        }

        private void VmReceipt_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {

            if (e.PropertyName == "Id")
            {
                Amount = 293.85F;
            }
        }

    }
}

输出消息显示以下内容:

System.Windows.Data Warning: 56 : Created BindingExpression (hash=39565849) for Binding (hash=50005411)
System.Windows.Data Warning: 58 :   Path: 'Receipt.Amount'
System.Windows.Data Warning: 62 : BindingExpression (hash=39565849): Attach to System.Windows.Controls.TextBox.Text (hash=29985742)
System.Windows.Data Warning: 67 : BindingExpression (hash=39565849): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=39565849): Found data context element: TextBox (hash=29985742) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=39565849): Activate with root item vmReceipt (hash=4309405)
System.Windows.Data Warning: 107 : BindingExpression (hash=39565849):   At level 0 using cached accessor for vmReceipt.Receipt: RuntimePropertyInfo(Receipt)
System.Windows.Data Warning: 104 : BindingExpression (hash=39565849): Replace item at level 0 with vmReceipt (hash=4309405), using accessor RuntimePropertyInfo(Receipt)
System.Windows.Data Warning: 101 : BindingExpression (hash=39565849): GetValue at level 0 from vmReceipt (hash=4309405) using RuntimePropertyInfo(Receipt): <null>
System.Windows.Data Warning: 106 : BindingExpression (hash=39565849):   Item at level 1 is null - no accessor
System.Windows.Data Warning: 80 : BindingExpression (hash=39565849): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 88 : BindingExpression (hash=39565849): TransferValue - using fallback/default value ''
System.Windows.Data Warning: 89 : BindingExpression (hash=39565849): TransferValue - using final value ''
System.Windows.Data Warning: 56 : Created BindingExpression (hash=31936960) for Binding (hash=23337408)
System.Windows.Data Warning: 58 :   Path: 'Receipt.Amount'
System.Windows.Data Warning: 62 : BindingExpression (hash=31936960): Attach to System.Windows.Controls.TextBox.Text (hash=57648048)
System.Windows.Data Warning: 67 : BindingExpression (hash=31936960): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=31936960): Found data context element: TextBox (hash=57648048) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=31936960): Activate with root item vmReceipt (hash=59679756)
System.Windows.Data Warning: 107 : BindingExpression (hash=31936960):   At level 0 using cached accessor for vmReceipt.Receipt: RuntimePropertyInfo(Receipt)
System.Windows.Data Warning: 104 : BindingExpression (hash=31936960): Replace item at level 0 with vmReceipt (hash=59679756), using accessor RuntimePropertyInfo(Receipt)
System.Windows.Data Warning: 101 : BindingExpression (hash=31936960): GetValue at level 0 from vmReceipt (hash=59679756) using RuntimePropertyInfo(Receipt): <null>
System.Windows.Data Warning: 106 : BindingExpression (hash=31936960):   Item at level 1 is null - no accessor
System.Windows.Data Warning: 80 : BindingExpression (hash=31936960): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 88 : BindingExpression (hash=31936960): TransferValue - using fallback/default value ''
System.Windows.Data Warning: 89 : BindingExpression (hash=31936960): TransferValue - using final value ''
System.Windows.Data Warning: 96 : BindingExpression (hash=31936960): Got PropertyChanged event from TextBox (hash=57648048) for DataContext
System.Windows.Data Warning: 79 : BindingExpression (hash=31936960): Deactivate
System.Windows.Data Warning: 103 : BindingExpression (hash=31936960): Replace item at level 0 with {NullDataItem}
System.Windows.Data Warning: 103 : BindingExpression (hash=31936960): Replace item at level 1 with {NullDataItem}
System.Windows.Data Warning: 78 : BindingExpression (hash=31936960): Activate with root item vmReceipt (hash=3436318)
System.Windows.Data Warning: 107 : BindingExpression (hash=31936960):   At level 0 using cached accessor for vmReceipt.Receipt: RuntimePropertyInfo(Receipt)
System.Windows.Data Warning: 104 : BindingExpression (hash=31936960): Replace item at level 0 with vmReceipt (hash=3436318), using accessor RuntimePropertyInfo(Receipt)
System.Windows.Data Warning: 101 : BindingExpression (hash=31936960): GetValue at level 0 from vmReceipt (hash=3436318) using RuntimePropertyInfo(Receipt): Receipt (hash=288552)
System.Windows.Data Warning: 107 : BindingExpression (hash=31936960):   At level 1 using cached accessor for Receipt.Amount: RuntimePropertyInfo(Amount)
System.Windows.Data Warning: 104 : BindingExpression (hash=31936960): Replace item at level 1 with Receipt (hash=288552), using accessor RuntimePropertyInfo(Amount)
System.Windows.Data Warning: 101 : BindingExpression (hash=31936960): GetValue at level 1 from Receipt (hash=288552) using RuntimePropertyInfo(Amount): '293.85'
System.Windows.Data Warning: 80 : BindingExpression (hash=31936960): TransferValue - got raw value '293.85'
System.Windows.Data Warning: 84 : BindingExpression (hash=31936960): TransferValue - implicit converter produced '293,85'
System.Windows.Data Warning: 89 : BindingExpression (hash=31936960): TransferValue - using final value '293,85'

由于值(293,85)显示在输出窗口(即跟踪信息)中但未显示在文本框本身中,我无法弄清楚,从何处开始?

1 个答案:

答案 0 :(得分:0)

您只需使用以下规范激活{em> 高级别的TraceLevel

, diag:PresentationTraceSources.TraceLevel=High}

所以它显示了为TextBox设置值的事件的非常详细的输出,正如预期的那样。

修改

在您编辑问题之后,我建议您不要多次设置DataContext,因此可能的更改可能是在c.tor中设置DataContext

    public Receipt()
    {
        InitializeComponent();
        vmEr = new vmReceipt(id);
        this.DataContext = vmEr;
    }

然后只更新属性。

    public int Id
    {
        get { return id; }
        set
        {
            id = value;
            vmEr.ID = id;
        }
    }