Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 如何在代码中而不是在xaml中设置ListBox.ItemsPanel?_Silverlight - Fatal编程技术网

Silverlight 如何在代码中而不是在xaml中设置ListBox.ItemsPanel?

Silverlight 如何在代码中而不是在xaml中设置ListBox.ItemsPanel?,silverlight,Silverlight,我在转换器中用C代码创建一个列表框,我决定必须显示哪个控件。不幸的是,我无法在C代码中将ItemsPanel设置为WrapPanel。目前,我有这样一个代码作为解决方案: 在xaml文件资源字典中: <ItemsPanelTemplate x:Key="HorizontalWrapPanelItemsPanelTemplate" > <toolkit:WrapPanel Orientation="Horizontal" /> </ItemsPanelTemp

我在转换器中用C代码创建一个列表框,我决定必须显示哪个控件。不幸的是,我无法在C代码中将ItemsPanel设置为WrapPanel。目前,我有这样一个代码作为解决方案:

在xaml文件资源字典中:

<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