【发布时间】:2021-04-23 03:43:25
【问题描述】:
我有以下 xaml 代码。
我正在尝试将自定义类 UnitCategory 绑定到组合框。
但似乎代码有问题。
<Grid>
<Grid.Resources>
<local:ComboSourceConverter x:Key="cscconv" />
<Style x:Key="UnitCategoryStyle" TargetType="{x:Type igEditors:XamComboEditor}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.UnitCategories, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<Setter Property="SelectedItem" Value ="{Binding Path=DataItem.UnitCategory}" />
<Setter Property="DisplayMemberPath" Value="Name" />
</Style>
<Style x:Key="UnitStyle" TargetType="{x:Type igEditors:XamComboEditor }">
<Setter Property="ItemsSource" Value="{Binding Path=DataItem.UnitCategory, Converter={StaticResource cscconv}}" />
</Style>
</Grid.Resources>
<igWPF:XamDataGrid DataSource="{Binding data}" CellChanged="XamDataGrid_CellChanged">
<igWPF:XamDataGrid.FieldLayoutSettings>
<igWPF:FieldLayoutSettings AutoGenerateFields="False" />
</igWPF:XamDataGrid.FieldLayoutSettings>
<igWPF:XamDataGrid.FieldLayouts>
<igWPF:FieldLayout>
<igWPF:FieldLayout.Fields>
<igWPF:Field Name="Item"></igWPF:Field>
<igWPF:Field Name="UnitCategory">
<igWPF:Field.Settings>
<igWPF:FieldSettings EditorStyle="{StaticResource UnitCategoryStyle}" />
</igWPF:Field.Settings>
</igWPF:Field>
<igWPF:Field Name="Unit">
<igWPF:Field.Settings>
<igWPF:FieldSettings EditorStyle="{StaticResource UnitStyle}" />
</igWPF:Field.Settings>
</igWPF:Field>
</igWPF:FieldLayout.Fields>
</igWPF:FieldLayout>
</igWPF:XamDataGrid.FieldLayouts>
</igWPF:XamDataGrid>
</Grid>
跟随视图模型。此处单元类别不具有约束力。组合框中的下拉菜单未显示。有绑定问题吗?
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<DataModel> data { get; set; }
public List<UnitCategory> UnitCategories { get; set; }
public static List<string> WeightUnits { get; set; }
public static List<string> DistanceUnits { get; set; }
public ViewModel()
{
this.data = new ObservableCollection<DataModel>();
this.UnitCategories = new List<UnitCategory>();
UnitCategory u1 = new UnitCategory();
u1.Name = "Wt";
UnitCategory u2 = new UnitCategory();
u2.Name = "Dt";
this.UnitCategories.Add(u1);
this.UnitCategories.Add(u2);
WeightUnits = new List<string>()
{
"mg",
"g",
"kg"
};
DistanceUnits = new List<string>()
{
"cm",
"m",
"km"
};
DataModel data1 = new DataModel();
data1.Item = "Item 1";
DataModel data2 = new DataModel();
data2.Item = "Item 2";
this.data.Add(data1);
this.data.Add(data2);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DataModel
{
public string Item { get; set; }
public UnitCategory UnitCategory { get; set; }
public string Unit { get; set; }
public DataModel()
{
}
}
public class UnitCategory
{
public string Name { get; set; }
}
【问题讨论】:
-
尝试将
AncestorType={x:Type Window}更改为AncestorType={x:Type igWPF:XamDataGrid} -
它不工作。实际上我们需要获取窗口数据上下文。因为对于 window -> VIewMODEl 是 datacontext。对于 Xamdatagrid -> 模型是数据上下文。对于组合框,它应该取自 viewmodel,它是 windows datacontext
-
对于
igWPF:XamDataGridDataContext是/应该是ViewModel,因为你绑定了DataSource="{Binding data}"或者你发布了错误的代码。 -
对于 XamdataGrid DataSource 是 DataModel 的列表,它是数据。对于窗口 Datacontext 是 viewModel 的实例
-
不,
DataModel的列表不是igWPF:XamDataGrid的DataContext,而是DataSource。
标签: c# wpf datagrid infragistics