数据绑定不更新

时间:2015-02-15 12:44:17

标签: c# wpf entity-framework data-binding

我有一组存储在客户列表中的实体,一个绑定到该列表的组合框和一个对象" SelectedCustomer"在组合框中获取所选客户,但问题是我有一个绑定到该对象的文本框,每当我在该对象中存储来自集合的新元素时,该文本框都不会更新

这是我的代码

public partial class MainWindow : Window 
{
    public MainWindow()
    {
        InitializeComponent();


        using (var context = new Entities())
        {

            Customers = context.PERFCONTENEUR.ToList();

            CustomersCombo.SelectedItem = Customers[0];

        }




        DataContext = this;

    }

    public List<PERFCONTENEUR> Customers { get; set; }
    public PERFCONTENEUR SelectedCustomer { get; set; }


    private void move(object sender, MouseButtonEventArgs e)
    {
        DragMove();



    }


    private void main1_Loaded(object sender, RoutedEventArgs e)
    {


    }

    private void CustomersCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        SelectedCustomer = CustomersCombo.SelectedItem as PERFCONTENEUR;





    }

    private void CustomersCombo_Loaded(object sender, RoutedEventArgs e)
    {

    }
}

这是我的XAML

 <TextBox Text="{Binding SelectedCustomer.ID}"  HorizontalAlignment="Left" Height="23" Margin="548,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <ComboBox  x:Name="CustomersCombo"
               ItemsSource="{Binding Customers}"
               DisplayMemberPath="CLIENT"
               SelectedValuePath="ID"

               HorizontalAlignment="Left" Margin="421,54,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="CustomersCombo_SelectionChanged" />

这是我在Perfconteneur类中实现的接口INotifyPropertyChanged,它是SelectedCustomer对象的类型

公共部分类PERFCONTENEUR:INotifyPropertyChanged

{   public event PropertyChangedEventHandler PropertyChanged;


    private string _CLIENT;



    public decimal ID { get; set; }
    public Nullable<decimal> TAILLE { get; set; }
    public string CLIENT
    {
        get { return _CLIENT; }
        set
        {
            if (value != _CLIENT)
            {
                _CLIENT = value;
                NotifyPropertyChanged("CLIENT");

            }
        }
    }



    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    public string D_ARRIVEE { get; set; }

    public virtual PERFREPARATION PERFREPARATION { get; set; }
}

1 个答案:

答案 0 :(得分:1)

当稍后更改时,您需要让SelectedCostumer属性通知UI;将INPC实现添加到MainWindow

private PERFCONTENEUR _selectedCustomer;
    public PERFCONTENEUR SelectedCustomer
    {
        get { return _selectedCustomer; }
        set
        {
            if (value != _selectedCustomer)
            {
                _selectedCustomer = value;
                NotifyPropertyChanged("SelectedCustomer");

            }
        }
    }

我不想更多地评论违反约定,尝试采用通用编码约定,因为其他程序员更容易理解您的代码