Silverlight 如何在代码中而不是在xaml中设置ListBox.ItemsPanel?
我在转换器中用C代码创建一个列表框,我决定必须显示哪个控件。不幸的是,我无法在C代码中将ItemsPanel设置为WrapPanel。目前,我有这样一个代码作为解决方案: 在xaml文件资源字典中:Silverlight 如何在代码中而不是在xaml中设置ListBox.ItemsPanel?,silverlight,Silverlight,我在转换器中用C代码创建一个列表框,我决定必须显示哪个控件。不幸的是,我无法在C代码中将ItemsPanel设置为WrapPanel。目前,我有这样一个代码作为解决方案: 在xaml文件资源字典中: <ItemsPanelTemplate x:Key="HorizontalWrapPanelItemsPanelTemplate" > <toolkit:WrapPanel Orientation="Horizontal" /> </ItemsPanelTemp
<ItemsPanelTemplate x:Key="HorizontalWrapPanelItemsPanelTemplate" >
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
它工作正常,但我将有如下代码:
listBoxEdit.ItemsPanel = new WrapPanel(); //Not Work
或
我有可能有这样的代码吗?或者exist是当前我的解决方案中更好的代码
tanx:在WPF中,您将使用FrameworkElementFactory:
FrameworkElementFactory factoryPanel = new FrameworkElementFactory(typeof(WrapPanel));
factoryPanel.SetValue(WrapPanel.OrientationProperty, Orientation.Horizontal);
ItemsPanelTemplate template = new ItemsPanelTemplate();
template.VisualTree = factoryPanel;
menu.ItemsPanel = template;
在Silverlight中,这将不起作用,您必须使用XAMLReader:
listBoxEdit.ItemsPanel = (ItemsPanelTemplate)XamlReader.Load(@"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' >
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>");
From:在WPF中,您将使用FrameworkElementFactory:
FrameworkElementFactory factoryPanel = new FrameworkElementFactory(typeof(WrapPanel));
factoryPanel.SetValue(WrapPanel.OrientationProperty, Orientation.Horizontal);
ItemsPanelTemplate template = new ItemsPanelTemplate();
template.VisualTree = factoryPanel;
menu.ItemsPanel = template;
在Silverlight中,这将不起作用,您必须使用XAMLReader:
listBoxEdit.ItemsPanel = (ItemsPanelTemplate)XamlReader.Load(@"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' >
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>");
发件人:好的,谢谢,但我认为我的代码更干净、更美观谢谢,但我认为我的代码更干净、更漂亮D