I'm pretty new to WPF and MVVM, so I might be missing some basic understanding.
I am trying to build a list of feed that can be ordered. Every order demands the name, an amount and a unit.
The units should be placed in a combobox, and that's the problem. Depending on the method, the combobox doesn't appear, or is just plain empty.
The following is the XAML.
<UserControl x:Class="TestProject.View.FruitFoodList"
DataContext="{Binding Test, Source={StaticResource Locator}}">
<Grid>
<DataGrid ItemsSource="{Binding DummyFruitList}" IsReadOnly="True" Name="dgFeedList" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Feed" Binding="{Binding Name}" Width="*"/>
<DataGridTemplateColumn Header="Amount">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<!-- Code for amount here -->
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridComboBoxColumn Header="Unit">
<!--Combobox for units here-->
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
Here's the viewmodel:
public class DummyViewModel : ViewModelBase
{
private List<DummyProduct> _dummyFruit = new List<DummyProduct>();
public List<DummyProduct> DummyFruitList
{
get { return _dummyFruit; }
set { _dummyFruit = value; }
}
public List<Unit> DummyUnitList = new List<Unit> {
new Unit { Name = "kg"},
new Unit { Name = "piece(s)" },
new Unit { Name = "box(es)" }
};
public DummyViewModel()
{
string[] lines = File.ReadAllLines("../../DummyFruit.txt", Encoding.UTF7);
foreach (string product in lines)
{
DummyProduct dummyProduct = new DummyProduct(product);
dummyProduct.Units = DummyUnitList;
DummyFruitList.Add(dummyProduct);
}
}
}
And DummyProduct:
public class DummyProduct
{
public DummyProduct(string product)
{
Name = product;
}
public string Name { get; set; }
public List<Unit> Units;
}
The units might not be the same for every product. I have, without luck, tried the following solutions: Binding WPF ComboBox to a Custom List Binding ItemsSource of a ComboBoxColumn in WPF DataGrid
The above solutions didn't show a combobox at all for me. But again, I might be doing it wrong.
Making a stackpanel with a combobox in a datagridtemplate has so far been the only solution that got a combobox to appear, but the bindings makes no sense here.