WPF组合框编辑器样式绑定不起作用



我有下面的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; }
}

我假设igWPF:Field或它的某些部分不在可视化树中。尝试创建一个ViewModel作为静态资源,然后绑定到StaticResource

<Window ... DataContext="{DynamicResource vmod}>
<Window.Resources>
<local:ViewModel x:Key="vmod" x:Shared="true"></local:ViewModel>"
</Window.Resources>
<Grid>
<Grid.Resources>
<Style x:Key="UnitCategoryStyle" TargetType="{x:Type igEditors:XamComboEditor}">
<Setter Property="Tag" Value ="{StaticResource vmod}"
<Setter Property="ItemsSource" Value="{Binding Tag.UnitCategories, RelativeSource={RelativeSource Mode=Self}}" />
<Setter Property="SelectedItem" Value ="{Binding Path=DataItem.UnitCategory}" />
<Setter Property="DisplayMemberPath" Value="Name" />
</Style>
</Grid.Resources>
<Grid>
...
</Window>

不要忘记删除DataContext之前的设置。

最新更新