wpf 动态添加子扩展器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11819675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:51:11  来源:igfitidea点击:

Dynamically add child expander

wpfdynamicexpander

提问by Ricardo Seixas

Is there a way to create child expanders like this xaml bellow, at run-time?

有没有办法在运行时创建像下面这样的 xaml 子扩展器?

<Expander Header="Building" IsExpanded="True">
    <StackPanel>
        <Expander Header="Sales">
            <StackPanel>
                <TextBlock>6100</TextBlock>
                <TextBlock>6101</TextBlock>
                <TextBlock>6111</TextBlock>
                <TextBlock>6150</TextBlock>
            </StackPanel>
        </Expander>
        <Expander Header="Director">
            <StackPanel>
                <TextBlock>6102</TextBlock>
                <TextBlock>6113</TextBlock>
            </StackPanel>
        </Expander>
    </StackPanel>
</Expander>

回答by Parham.D

Set a name for first StackPanel (MainStackPanel) under the top Expander.

为顶部扩展器下的第一个 StackPanel (MainStackPanel) 设置一个名称。

// Add new expander, stack panel and text block.
var newExpander = new Expander {Name = "NewExpander", Header = "New Expander"};
var newstackPanel = new StackPanel {Name = "NewExpanderStackPanel"};
var newtextBlock = new TextBlock {Text = "777"};

// Add above items as children.     
newstackPanel.Children.Add(newtextBlock);
newExpander.Content = newstackPanel;
MainStackPanel.Children.Add(newExpander);

回答by Eli Arbel

The best way would probably be to use data binding. I'm assuming you have some data structure that describes which expanders you want to create. Let's assume we have:

最好的方法可能是使用数据绑定。我假设您有一些数据结构来描述您想要创建的扩展器。让我们假设我们有:

class Building
{
    public List<Item> Items { get; }
    public string Name { get; }
}

class Item
{
    public int Number { get; }
}

In our user control, we'll set the DataContextto a list of Buildings (e.g. in the constructor):

在我们的用户控件中,我们将设置DataContextBuildings的列表(例如在构造函数中):

DataContext = GetListOfBuildings();

Then we'd use two nested ItemsControls with templates to create the controls at runtime:

然后我们将使用两个ItemsControl带有模板的嵌套s 在运行时创建控件:

<Expander Header="Building"
          IsExpanded="True">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Expander Header="{Binding Name}">
                    <ItemsControl ItemsSource="{Binding Items}"
                                  DisplayMemberPath="Number" />
                </Expander>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Expander>

This way you would not need to create controls in C#, which is more cumbersome in WPF.

这样你就不需要在 C# 中创建控件,这在 WPF 中更麻烦。

回答by Charles Liu

Not quite sure about what you want to achieve.

不太确定你想要实现什么。

If I understand correctly, you can save your code in a .xaml file as resource and use application to load it. Then, add the object to the visual tree.

如果我理解正确,您可以将代码保存在 .xaml 文件中作为资源并使用应用程序加载它。然后,将对象添加到可视化树中。

  var expander = (Expander) Application.LoadComponent(new Uri("UriToTheXamlFile"));
  control.AddChild(expander);