wpf 将 StackPanel 子项绑定到 ObservableCollection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16773037/
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 08:52:31  来源:igfitidea点击:

Binding StackPanel Children to an ObservableCollection

c#wpfxamlbindingstackpanel

提问by Sturm

I have an ObservableCollection of buttons:

我有一个 ObservableCollection 按钮:

public partial class MainWindow : Window   
     {
        public ObservableCollection<Button> myBtCollection { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            myBtCollection = new ObservableCollection<Button>();

            Button bot1 = new Button { Width = 200, Height = 25, Content = "Boton 1" };
            Button bot2 = new Button { Width = 150, Height = 50, Content = "Boton 2" };
            Button bot3 = new Button { Width = 100, Height = 100, Content = "Boton 3" };

            myBtCollection.Add(bot1);
            myBtCollection.Add(bot2);
            myBtCollection.Add(bot3);
            myBtCollection.Add(bot1);
            myBtCollection.Add(bot3);
            myBtCollection.Add(bot2);
            myBtCollection.Add(bot1);
        }
    }

I want to bind that collection to my StackPanel (in this example it's a constant collection but eventually it would be variable). This is my XAML:

我想将该集合绑定到我的 StackPanel(在本例中它是一个常量集合,但最终它会是可变的)。这是我的 XAML:

<Window x:Name="mainWindow" x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <StackPanel x:Name="stack">

        </StackPanel>

        <ItemsControl  Width="Auto" 
                       Height="Auto"
                       ItemsSource="{Binding ElementName=mainWindow, Path=myBtCollection}">      
        </ItemsControl>


    </Grid>
</Window>

I've read that it can be achieved by using ItemsControl, but I don't know how to finish it. (Do I need to set DataContext in code behind?)

我已经读到它可以通过使用 ItemsControl 来实现,但我不知道如何完成它。(我需要在后面的代码中设置 DataContext 吗?)

回答by LPL

I agree with the comment from @inxs. But to make this work move InitializeComponent()after the creation of myBtCollection

我同意@inxs 的评论。但是要InitializeComponent()在创建 myBtCollection 之后让这项工作动起来

public MainWindow()
{
    myBtCollection = new ObservableCollection<Button>();
    ...

    InitializeComponent();
}

or implement INotifyPropertyChangedfor myBtCollection.

或者实现INotifyPropertyChanged接口myBtCollection

回答by Steven S.

  1. The ItemsControl uses a Vertical StackPanel already.
  2. You can't bind data to a StackPanel, which is used for the visual layout.
  1. ItemsControl 已经使用了 Vertical StackPanel。
  2. 您无法将数据绑定到用于可视化布局的 StackPanel。

If you wish to use a different Panel or change the StackPanels orientation you could use the Property "ItemsPanel" on the ItemsControl and set it like this:

如果您希望使用不同的面板或更改 StackPanels 方向,您可以使用 ItemsControl 上的属性“ItemsPanel”并将其设置如下:

<ItemsControl.Style>
            <Style TargetType="{x:Type ItemsControl}">
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
 </ItemsControl.Style>