简体   繁体   中英

MVVM ListView & TextBox binding SelectedItem

I want to bind the ListViews SelectedItem to my TextBox..What i do is the following:

        private User _selectedUser;
        public User SelectedUser
        {
            get { return _selectedUser; }
            set
            {
                _selectedUser = value;
                RaisePropertyChanged("SelectedUser");
            }
        }

ListView Xaml:

    <ListView HorizontalAlignment="Left" 
              Height="194"
              Margin="37,102,0,0" 
              SelectedItem="{Binding SelectedItem}"

Textbox Xaml:

    <TextBox Text="{Binding SelectedItem.FirstName}" />

If i selected an entry from the listview it displays the selected entries name value in the textbox. That's how it should be, no problem.

The issue is: I want to edit the text in the textbox and when i press a button it should update the selectedentries name with the name i typed into the textbox. It should only do that if i press the button, but right now it already changes the entries name when i lose focus from the textbox.

How can i fix it? I already tried all the different Modes but nothing seem to work

It is doing exactly what you are telling it to do. Both your ListView and the TextBox are bound to exactly the same thing. So when you change one, the change reflects immediately.

If you want to achieve what you sound like you are trying to, you are going to need to copy the FirstName to another property (lets call it SelectedFirstName ) , allow the user to edit that value in the TextBox and then when they user clicks a button, update the selected item's FirstName property with the value from SelectedFirstName

From MSDN :

The TextBox.Text property has a default UpdateSourceTrigger value of LostFocus. This means if an application has a TextBox with a data-bound TextBox.Text property, the text you type into the TextBox does not update the source until the TextBox loses focus (for instance, when you click away from the TextBox).

If you want your source to be updated at another time (not when your TextBox loses focus) you should play with UpdateSourceTrigger property. For example, you can state <TextBox Text="{Binding SelectedItem.FirstName, UpdateSourceTrigger=Explicit}" /> , and then call UpdateSource method only when you press your button. Read the documentation link for details:

If you have a dialog or a user-editable form and you want to defer source updates until the user is finished editing the fields and clicks "OK", you can set the UpdateSourceTrigger value of your bindings to Explicit, as in the following example:

<TextBox Name="itemNameTextBox"
         Text="{Binding Path=ItemName, UpdateSourceTrigger=Explicit}" />

When you set the UpdateSourceTrigger value to Explicit, the source value only changes when the application calls the UpdateSource method. The following example shows how to call UpdateSource for itemNameTextBox:

// itemNameTextBox is an instance of a TextBox
BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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