简体   繁体   English

当选择更改时,如何停止Combobox更新源数据?

[英]How to STOP Combobox from updating source data when the selection changes?

I have a ComboBox displaying people, a TextBox for firstname, a TextBox for lastname, a Button to save the entered data. 我有一个ComboBox显示的人,一个TextBox的名字,一个TextBox的姓氏,一个Button来保存输入的数据。 The user may enter what ever he wants in the TextBox es and when he clicks the 'save' button the contents of the TextBox es should be saved to the currently selected person. 用户可以在TextBox输入他想要的内容,并且当他单击“保存”按钮时, TextBox的内容应保存到当前选定的人。

This is how my TextBox es look like in XAML: 这就是我的TextBox在XAML中的样子:

<Grid>
    <Combobox x:Name="cbPerson" ItemsSource={Binding Source={StaticResource collection_people}
          SelectedValue={Binding Path=Firstname} DisplayMemberPath={Binding Path=Firstname}
          SelectedValuePath={Binding Path=Firstname}/>

    <Textbox x:Name="tbFirstname" Text="{Binding Path=name, Mode=TwoWay,
          UpdateSourceTrigger=Explicit}}" Width="Auto"/>

    <Textbox x:Name="tbLastname" Text="{Binding Path=name, Mode=TwoWay, 
          UpdateSourceTrigger=Explicit}}" Width="Auto">
</Grid>

This is how my Button function looks like: 这是我的Button函数的样子:

private void btnSave_Click(object sender, RoutedEventArgs e)
{
    tbFirstname.GetBindingExpression(TextBox.TextProperty).UpdateSource();
    tbLastname.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}

In my Person class I implemented INotifyPropertyChanged interface in order for the TwoWay binding to work. 在我的Person类中,我实现了INotifyPropertyChanged接口,以便TwoWay绑定工作。

When I select an item from the ComboBox the TextBox es get filled with the according data. 当我从ComboBox选择一个项目时, TextBox将填充相应的数据。 I can then edit the TextBox es and save the data to the object by pressing the save button. 然后,我可以编辑TextBox es并通过按保存按钮将数据保存到对象。 Everything works as you would expect except for one thing: 除了一件事,一切都如您期望的那样工作:

For instance, when I select "Paul" from the ComboBox and change the text of the firstname TextBox from "Paul" to "Leo" and then select another item from the ComboBox WITHOUT clicking the save button, the object previous object ("Paul") gets updated nevertheless. 例如,当我从ComboBox选择“Paul”并将名字TextBox“Paul”更改为“Leo”然后从ComboBox选择另一个项目而不单击保存按钮,对象前一个对象(“Paul” )仍会更新。

Why is that and how can I fix this? 为什么这样,我该如何解决这个问题? I haven't had any success finding something related to this problem. 我没有找到与此问题相关的任何成功。

Can you try this for your XAML code? 你可以试试这个XAML代码吗?

<Grid>
    <ComboBox x:Name="cbPerson" ItemsSource="{Binding Source={StaticResource collection_people}}"
              DisplayMemberPath="FirstName"/>

    <TextBox x:Name="tbFirstName" 
             Text="{Binding Path=SelectedItem.FirstName, ElementName=cbPerson, Mode=TwoWay,
                            UpdateSourceTrigger=Explicit}" 
             Width="Auto"/>

    <TextBox x:Name="tbLastName" 
             Text="{Binding Path=SelectedItem.LastName, ElementName=cbPerson, Mode=TwoWay, 
                            UpdateSourceTrigger=Explicit}" 
             Width="Auto"/>
</Grid>

Your click event handler be like, 您的click事件处理程序如,

private void btnSave_Click(object sender, RoutedEventArgs e)
{
    tbFirstName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
    tbLastName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}

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

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