我目前正在使用WPF .NET 4.7应用程序,并且使用Infragistics WPF控件版本18。
我需要创建一个自定义XamComboEditor
,其中包含XamDataTree
。因此,内部具有树选择的组合框。
没有XamComboEditor的Tree选择效果很好,看起来像这样:
<iWPF:XamDataTree ItemsSource="{Binding Locations}">
<iWPF:XamDataTree.GlobalNodeLayouts>
<iWPF:NodeLayout Key="Locations" TargetTypeName="LocationViewModel" DisplayMemberPath="Name"/>
<iWPF:NodeLayout Key="ChildLocations" TargetTypeName="string"/>
</iWPF:XamDataTree.GlobalNodeLayouts>
</iWPF:XamDataTree>
我的XamDataTree绑定到可观察的集合Locations
:
public ObservableCollection<LocationViewModel> Locations { get; set; } = new ObservableCollection<LocationViewModel>();
public class LocationViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public List<LocationViewModel> ChildLocations { get; set; } = new List<LocationViewModel>();
}
我需要在XamComboEditor
上使用样式设置器,将XamDataTree
放在组合框中。
我的问题是现在,我不知道如何实现此目标,或者如何将上下文从XamComboEditor
进一步传递给XamDataTree
。
我徒劳地尝试了以下内容:
<iWPF:XamComboEditor ItemsSource="{Binding Locations}">
<iWPF:XamComboEditor.ComboBoxStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<iXaml:XamDataTree ItemsSource="{Binding .}">
<iXaml:XamDataTree.GlobalNodeLayouts>
<iXaml:NodeLayout Key="Locations" TargetTypeName="LocationViewModel" DisplayMemberPath="{Binding Name}"/>
<iXaml:NodeLayout Key="ChildLocations" TargetTypeName="string"/>
</iXaml:XamDataTree.GlobalNodeLayouts>
</iXaml:XamDataTree>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</iWPF:XamComboEditor.ComboBoxStyle>
</iWPF:XamComboEditor>
您知道如何解决此问题吗?您是否知道如何将数据上下文从父控件传递到子控件?或者更确切地说,如何将XamDataTree
放在XamComboEditor
内?
如果我正确理解,则您的DataContext
的XamlDataTree
与您期望的不一样(Locations
绑定在您的XamComboEditor
中)。
解决此问题的一种方法是在Binding
标记扩展名中指定路径的来源。您可以使用{x:Reference ...}
标记扩展名来引用控件树中的已命名控件。
<iWPF:XamComboEditor x:Name="comboEditor" ItemsSource="{Binding Locations}">
<iWPF:XamComboEditor.ComboBoxStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<iXaml:XamDataTree ItemsSource="{Binding Source={x:Reference Name=comboEditor}, Path=DataContext.Locations}">
<iXaml:XamDataTree.GlobalNodeLayouts>
<iXaml:NodeLayout Key="Locations" TargetTypeName="LocationViewModel" DisplayMemberPath="{Binding Name}"/>
<iXaml:NodeLayout Key="ChildLocations" TargetTypeName="string"/>
</iXaml:XamDataTree.GlobalNodeLayouts>
</iXaml:XamDataTree>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</iWPF:XamComboEditor.ComboBoxStyle>
</iWPF:XamComboEditor>
您也可以在不使用Binding中的RelativeSource
属性命名控件的情况下(例如RelativeSource={RelativeSource AncestorType=iWPF:XamComboEditor}
之类。)>