I am new to the MVVM pattern, and a little confused on when to use Code Behind. I have a very simple form right now, that includes one TextBox, and one DataGrid. What I would like is to be able to have the DataGrid change its selected item based on the TextBox.
I have done this in Code Behind and it works fine using the following code:
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
for (int i = 0; i < dataGrid1.Items.Count; i++)
{
string cellContent = dtReferral.Rows[i][0].ToString();
try
{
if (cellContent != null && cellContent.Substring(0, textBox1.Text.Length).Equals(textBox1.Text))
{
object item = dataGrid1.Items[i];
dataGrid1.SelectedItem = item;
dataGrid1.ScrollIntoView(item);
//row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
break;
}
}
catch { }
}
}
Now, I just want to highlight the Item in the Datagrid that starts with text in textbox, and allow the user to press a button to edit selected item.
Is it okay to have this logic in the Code Behind file? Or would I need to do this through some sort of binding? If I should do this through the View Model with Binding, any direction would be appreciated. Thank you.
I have been using MVVM for quiet a while now, and I still prefer using at as a guideline rather than a strict practice, partly because it isn't always practical to do everything in MVVM pattern exactly, and even more so if you are not to familiar with it.
I would suggest just playing around with it until you manage to find a form of MVVM that suits you.
I don't believe it is taboo to have Code in the Code Behind of the MVVM if the code is UI related.
ScrollIntoView isn't a Bindable property so you if you want to bind to it you will have to create a dependency Property to indirectly handle the binding. As for setting the selected item you could do it through something like:
View:
ViewModel:
If you only want to highlight the cells with the text from the
TextBox
you could make anAttatchedProperty
for theDataGrid
to accept your search value from theTextBox
and create anotherAttatchedProperty
for theCell
to indicate a match that you can usee to set properties in theCell
style. Then we create aIMultiValueConverter
to check theCell
value for a match to the searchText
.This way its reusable on other projects as you only need the
AttachedProperties
andConverter
Bind the
AttachedProperty
SearchValue
to yourTextBox
Text
property.Then create a
Style
forDataGridCell
and create a Setter for theAttachedProperty
IsTextMatch
using theIMultiValueConverter
to return if the cells text matches theSearchValue
Then we can use the
Cells
attachedIsTextMatch
property to set a highlight using aTrigger
Here is a working example showing my rambilings :)
Code:
Xaml:
Result:
Edit:
If you just want to select the row based on a single Column you can modify quite easily :).
Override the Style of
DataGridRow
instead ofDataGridCell
.First pass in the property you want into the
IMultiValueConverter
this should be yourDataContext
Then change the
Trigger
to setIsSelected
on theRow
Should look like this:
Result: