XAML代码与C#代码转换

最近遇到一个问题,需要动态绑定数据。这样的话不能在前端给写死了,但是有一些XAML代码因个人能力问题,改写不成C#代码。请教大家
XAML代码如下:

<ListBox Background="Transparent"
         HorizontalAlignment="Left"
         Margin="30,0"
         BorderThickness="0"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         ItemContainerStyle="{StaticResource RunwayListBoxItemStyle}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

这XAML代码,出来的样式是一个每行可以放多个元素的ListBox。我的问题是 ListBox.ItemsPanel以后的部分不知道该怎样写成C#代码。ListBox.ItemPanel以前的部分倒是可以直接的 . 出来。请教后面的部分应该怎样写

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7731525
  • 你也可以参考下这篇文章:WPF——为ListBox的Items设置布局面板
  • 以下回答来自chatgpt:

    答案:

    要在C#代码中实现与上述XAML样式相同功能的ListBox,需要进行以下步骤:

    1. 创建一个WrapPanel对象,并将其作为ListBox的ItemsPanel模板。

    WrapPanel panel = new WrapPanel(); listBox.ItemsPanel = new ItemsPanelTemplate(panel);

    1. 创建一个DataTemplate对象,并在其内部创建一个TextBlock控件。这个DataTemplate对象将用于绑定ListBox的每个元素。

    DataTemplate dataTemplate = new DataTemplate(); FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock)); Binding binding = new Binding(); binding.Path = new PropertyPath("."); factory.SetBinding(TextBlock.TextProperty, binding); dataTemplate.VisualTree = factory;

    1. 创建一个ListBoxItem对象,并将其Header属性设置为一个字符串值。将DataTemplate对象赋值给ListBoxItem的Content属性。

    ListBoxItem item = new ListBoxItem(); item.Header = "item"; item.ContentTemplate = dataTemplate;

    1. 将ListBoxItem对象添加到ListBox控件中。

    listBox.Items.Add(item);

    最终的代码实现如下:

    WrapPanel panel = new WrapPanel(); listBox.ItemsPanel = new ItemsPanelTemplate(panel);

    DataTemplate dataTemplate = new DataTemplate(); FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock)); Binding binding = new Binding(); binding.Path = new PropertyPath("."); factory.SetBinding(TextBlock.TextProperty, binding); dataTemplate.VisualTree = factory;

    for (int i = 0; i < data.Count; i++) { ListBoxItem item = new ListBoxItem(); item.Header = i.ToString(); item.ContentTemplate = dataTemplate; listBox.Items.Add(item); }

    其中,data是一个包含了需要绑定到列表中的数据的集合。通过上述代码,可以实现ListBox样式为每行可以放置多个元素的功能。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

是这样的吗:

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)FindResource("RunwayListBoxItemStyle");

WrapPanel wrapPanel = new WrapPanel();
wrapPanel.IsItemsHost = true;

listBox.ItemsPanel = new ItemsPanelTemplate(new FrameworkElementFactory(typeof(WrapPanel)));

container.Children.Add(listBox);

要将XAML代码转换为C#代码,您可以使用.NET的编程模型。对于您提到的ListBox.ItemsPanel部分,可以使用以下C#代码来动态创建和设置:

using System.Windows.Controls;

// 创建ListBox
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.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);
listBox.ItemContainerStyle = (Style)FindResource("RunwayListBoxItemStyle"); // 替换为您的样式资源

// 创建ItemsPanelTemplate
ItemsPanelTemplate itemsPanelTemplate = new ItemsPanelTemplate();

// 创建WrapPanel并设置IsItemsHost属性
WrapPanel wrapPanel = new WrapPanel();
wrapPanel.SetValue(WrapPanel.IsItemsHostProperty, true);

// 将WrapPanel设置为ItemsPanelTemplate的VisualTree
itemsPanelTemplate.VisualTree = wrapPanel;

// 将ItemsPanelTemplate设置为ListBox的ItemsPanel
listBox.ItemsPanel = itemsPanelTemplate;

请注意,上述代码中的FindResource("RunwayListBoxItemStyle")是一个占位符,请根据您的实际情况替换为ListBoxItem的样式资源。

希望这可以帮助到您!