Álvaro García
Álvaro García

Reputation: 19396

is it possible the binding to two properties?

I have a view model that has two properties. One of them is myDataGridSelectedItems, that is update in the selection changed event of the datagrid (I am using MVVM light to convert an event to command).

The second property is myText, that is the text that has a textbox in the view.

In my view I have a textBox which text depends on the selection of the dataGrid, if the selection is one item then I put the information of a column of the dataGrid in the textBox, if the selection is 0 or more than 1, then I clear the textBox.

To do that, I use the following code:

<TextBox Height="23" HorizontalAlignment="Stretch" Margin="5,26,0,0" Name="mytextBox" VerticalAlignment="Top"
                 Text="{Binding ElementName=Principal, Path=DataContext.MyDatagridSelectedItems, Converter={StaticResource TextBoxValueConverter}}">

This works fine because when I select one row in the data grid the textBox has text (the text that the convert returns) and is empty when I select more that one or unselect all rows.

However, in this way the property myText is not update because I don't set the binding, because the binding of the Text property in the axml use the converter, not the property myText of the view model.

So I was wondering if it possible to set two bindings in the Text property of the textBox, or if exists some way to update the myText property in the view model when the text in the TextBox changes.

Thanks.

Upvotes: 0

Views: 85

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174457

You are doing it the wrong way around: Right now, you have view logic encoded in a converter in the view. But view logic is precisly what the view model is there for.

You should have a property for the text of that text box in the view model and bind the text box only to that property.
In the view model you change its value according to the selection.

Upvotes: 1

Related Questions