0

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.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193

1 Answers1

1

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.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Well, perhaps in this case you a re right, but I think that the logic of the view should be in the vew. For example, if I want to hide a control when I check a checkbox. If the view model shouldn't know anything about the view, I shouldn't have a bool property in the view model to control when to hide or not the control. This should do in the view in some way. – Álvaro García May 06 '13 at 14:17
  • 2
    @ÁlvaroGarcía: I disagree. What triggers the hiding of the checkbox? If it is something that is or needs to be known by the view model, it belongs there. – Daniel Hilgarth May 06 '13 at 14:19
  • I think that view model control the data, but how to show this data I think that is responsability of the view. Because, If I have two skins (two version of a window) that operates with the same data, if the view model know nothing about the view, I could have one view model and two views, but if the view model control de visual aspect to control de view, then I need two view models and two views, because perhaps one view want to hide a control and the other one not. If I control this aspecti in the view model, I need two view models. – Álvaro García May 06 '13 at 14:31
  • @ÁlvaroGarcía: If the information is only for the view and not for the view model you are right: It doesn't belong into the view model. But in that case you wouldn't have the question you asked here. If it is just relevant for the view it doesn't need to be bound to a property in the view model. – Daniel Hilgarth May 06 '13 at 14:52