<ListBox Background="Transparent" HorizontalAlignment="Left" Margin="30,0" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemContainerStyle="{StaticResource RunwayListBoxItemStyle}" ItemsSource="{Binding SearchedResultRunwayCollection}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel IsItemsHost="True"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox> 将上述XAML代码转换成C#代码
时间: 2024-03-13 12:43:17 浏览: 16
你可以使用以下C#代码来创建ListBox并设置其属性:
```csharp
ListBox listBox = new ListBox();
listBox.Background = Brushes.Transparent;
listBox.HorizontalAlignment = HorizontalAlignment.Left;
listBox.Margin = new Thickness(30, 0, 0, 0);
listBox.BorderThickness = new Thickness(0);
listBox.ScrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
listBox.ItemContainerStyle = (Style)Application.Current.Resources["RunwayListBoxItemStyle"];
listBox.ItemsSource = SearchedResultRunwayCollection;
ItemsPanelTemplate itemsPanelTemplate = new ItemsPanelTemplate();
FrameworkElementFactory wrapPanelFactory = new FrameworkElementFactory(typeof(WrapPanel));
wrapPanelFactory.SetValue(WrapPanel.IsItemsHostProperty, true);
itemsPanelTemplate.VisualTree = wrapPanelFactory;
listBox.ItemsPanel = itemsPanelTemplate;
```
注意,你需要将`SearchedResultRunwayCollection`替换为你的数据源。另外,`RunwayListBoxItemStyle`是在XAML中定义的样式,你需要在C#代码中引用它。