WPF CLR类型到UIElement
我正在努力解决一个复杂的问题。我正在构建一个动态界面,希望将WPF CLR类型到UIElement,wpf,uielement,Wpf,Uielement,我正在努力解决一个复杂的问题。我正在构建一个动态界面,希望将ArrayList的任何存在转换为TreeView。我尝试了值转换器,但它不起作用 这是我的密码: if(current.Value is ArrayList) { var factory = new FrameworkElementFactory(typeof (TreeView)); factory.SetBinding(TreeView.ItemsSourceProperty, new Binding("
ArrayList
的任何存在转换为TreeView
。我尝试了值转换器,但它不起作用
这是我的密码:
if(current.Value is ArrayList)
{
var factory = new FrameworkElementFactory(typeof (TreeView));
factory.SetBinding(TreeView.ItemsSourceProperty, new Binding("[" + current.Key + "]"));
factory.SetBinding(TreeView.DisplayMemberPathProperty, new Binding("[" + current.Key + "][0].Text"));
template = new DataTemplate() {VisualTree = factory};
}
var column = new GridViewColumn
{
Header = current.Key,
CellTemplate = template
};
gridView.Columns.Add(column);
ArrayList
包含的项是Dictionary
,然后Dictionary包含项。是否有理由不声明这样做?您可以非常轻松地为项目创建模板:
<DataTemplate x:Key="ArrayListTemplate">
<TreeView>
<TreeView.ItemsSource>
<Binding Source="[{Binding Key}]"/>
</TreeView.ItemsSource>
<TreeView.DisplayMemberPath>
<Binding Source="[{Binding Key}][0].Text"/>
</TreeView.DisplayMemberPath>
</TreeView>
</DataTemplate>
我承认,我不确定上面定义的绑定是否有效。但是,假设他们这样做了,您唯一的另一个问题是仅将此模板应用于
值
为数组列表
的项目。您应该能够编写这样做的模板选择器,并将其分配给GridViewColumn.CellTemplateSelector
是否有理由不以声明方式执行此操作?您可以非常轻松地为项目创建模板:
<DataTemplate x:Key="ArrayListTemplate">
<TreeView>
<TreeView.ItemsSource>
<Binding Source="[{Binding Key}]"/>
</TreeView.ItemsSource>
<TreeView.DisplayMemberPath>
<Binding Source="[{Binding Key}][0].Text"/>
</TreeView.DisplayMemberPath>
</TreeView>
</DataTemplate>
我承认,我不确定上面定义的绑定是否有效。但是,假设他们这样做了,您唯一的另一个问题是仅将此模板应用于
值
为数组列表
的项目。您应该能够编写这样做的模板选择器,并将其分配给GridViewColumn.CellTemplateSelector
您是否尝试了DataType为ArrayList的DataTemplate?是否尝试了DataType为ArrayList的DataTemplate?我使用了上述技术,但绑定不起作用。我将在几个小时后发布一些代码。我使用了上述技术,但绑定不起作用。我会在几个小时后发布一些代码。