我对WPF和MVVM很陌生,所以我可能会缺少一些基本的理解。
我正在尝试构建可以订购的Feed列表。每个订单都需要名称,数量和单位。
单元应放在组合框中,这就是问题所在。根据方法的不同,组合框不会出现,或者只是空白。
以下是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>
这里是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);
}
}
}
和DummyProduct:
public class DummyProduct
{
public DummyProduct(string product)
{
Name = product;
}
public string Name { get; set; }
public List<Unit> Units;
}
每种产品的单位可能不同。 我没有运气,尝试了以下解决方案: Binding WPF ComboBox to a Custom List Binding ItemsSource of a ComboBoxColumn in WPF DataGrid
上述解决方案根本没有为我显示组合框。 但同样,我可能做错了。
在datagridtemplate中创建一个带有组合框的堆栈面板到目前为止唯一能够出现组合框的解决方案,但绑定在这里没有任何意义。
答案 0 :(得分:0)
ComboBox的ItemsSource缺失,请使用ObservableCollection而不是List。那应该有用;
<DataGridComboBoxColumn Header="Unit" >
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.DummyUnitList}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
答案 1 :(得分:0)
XAML
<DataGridComboBoxColumn Header="Unit" DisplayMemberPath="Name" SelectedValuePath="Id" SelectedValueBinding="{Binding UnitId, UpdateSourceTrigger=PropertyChanged}" MinWidth="280" Width="Auto" IsReadOnly="False">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DummyUnitList, UpdateSourceTrigger=PropertyChanged}"/>
<Setter Property="Width" Value="280" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DummyUnitList, UpdateSourceTrigger=PropertyChanged}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
SelectedValueBinding="{Binding UnitId, UpdateSourceTrigger=PropertyChanged}"
其中UnitId
是您DataGrid ItemsSource的元素,这是您选择存储在数据库中的值(或从中读取)
DisplayMemberPath="Name"
SelectedValuePath="Id"
其中Id
和Name
是Unit
类的属性
DummyUnitList 应该是ObservableCollection<>
而不是List<>
public ObservableCollection<Unit> DummyUnitList
{
get => _dummyUnitList ;
set { _dummyUnitList = value; OnPropertyChanged(nameof(DummyUnitList)); }
}