我需要显示来自一个类的XamDataGrid
数据,该类包含一些属性和一个有效选项列表。类似的东西:
public class DataViewModel
{
public string Identifier {get;private set;}
public string Value {get;set;}
public IEnumeranble<string> ValidValues {get; set;}
}
我正在尝试将设置设置为XamDataGrid
,以便通过Identifier
编辑一个不可编辑的Value
列和XamComboEditor
列。这是我提出的xaml:
<igDP:XamDataGrid GroupByAreaLocation="None">
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AutoGenerateFields="False"/>
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:FieldLayout.Fields>
<igDP:Field Name="Identifier"/>
<igDP:Field Name="Value">
<igDP:Field.Settings>
<igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}">
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igEditors:XamComboEditor}">
<Setter Property="IsEditable" Value="False" />
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.DataItem.Values,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type igEditors:XamComboEditor}}}" />
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:Field.Settings>
</igDP:Field>
</igDP:FieldLayout.Fields>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
XamDataGrid
上的数据绑定工作正常,但XamComboEditor.ItemsSource
上的数据绑定不起作用,但我不确定如何修复ID。你有什么想法吗?
答案 0 :(得分:2)
我找到了另一种使用事件的方法。
在XAML中,我必须指定那些设置而不是我的设置:
<igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}">
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igEditors:XamComboEditor}">
<Style.Setters>
<EventSetter Event="Loaded" Handler="XCEloaded"/>
<Setter Property="IsEditable" Value="False" />
</Style.Setters>
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
我用这段代码处理了这个事件:
private void XCEloaded(object sender, RoutedEventArgs e)
{
var drp = Utilities.GetAncestorFromType(sender as DependencyObject, typeof(DataRecordPresenter), true) as DataRecordPresenter;
if (drp.DataRecord.DataItem != null)
(sender as XamComboEditor).ItemsSource = (drp.DataRecord.DataItem as DataViewModel).ValidValues;
}
它看起来不是最干净的解决方案,但至少它可行。