Your front code:
- set your model namespace
xmlns:viewModel="clr-namespace:CPL3_workstation.MVVM.ModelViews.Tables"
- set
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
, because d:DataContext="{d:DesignInstance...}
<DataGrid d:DataContext = "{d:DesignInstance viewModel:TestViewModel, IsDesignTimeCreatable = True}"
ItemsSource = "{Binding Rows}"
SelectedItem = "{Binding Selector}">
<DataGrid.Columns>
<DataGridTextColumn
Binding = "{Binding Content1}"
Header = "Column 1" />
<DataGridTextColumn
Binding = "{Binding Content2}"
Header = "Column 2" />
<DataGridTextColumn
Binding = "{Binding Content3}"
Header = "Column 3" />
</DataGrid.Columns>
</DataGrid>
Your ViewModel
and the Model
below:
namespace CPL3_workstation.MVVM.ModelViews.Tables
{
public class TestViewModel
{
public IEnumerable<TestModel> Rows { get; set; }
public TestModel Selector
{
get => selector;
set => selector = value;
}
private TestModel selector;
public TestViewModel()
{
Rows = new List<TestModel>
{
new TestModel{ Content1 = "one", Content2 = "two", Content3 = "three" },
new TestModel{ Content1 = "one", Content2 = "two", Content3 = "three" },
new TestModel{ Content1 = "one", Content2 = "two", Content3 = "three" },
new TestModel{ Content1 = "one", Content2 = "two", Content3 = "three" }
};
}
}
public class TestModel
{
public string Content1 { get; set; }
public string Content2 { get; set; }
public string Content3 { get; set; }
}
}
As you can see, the SelectedItem
has a binding to the Selector
property of the type of your model.