C# 如何绑定ObservableCollection<;T>;去包裹?

C# 如何绑定ObservableCollection<;T>;去包裹?,c#,wpf,data-binding,observablecollection,C#,Wpf,Data Binding,Observablecollection,我用的是WPF。我有ToggleButton的observedcollection: private ObservableCollection<ToggleButton> myg = new ObservableCollection<ToggleButton>(); 有可能吗?如果有,怎么可能?也许还有其他类似的方法可以做到这一点 这很容易,但有一点需要注意: 要利用“observable collection”功能,需要绑定到它,但WrapPanel上没有诸如Item

我用的是WPF。我有
ToggleButton
observedcollection

private ObservableCollection<ToggleButton> myg = new ObservableCollection<ToggleButton>();
有可能吗?如果有,怎么可能?也许还有其他类似的方法可以做到这一点


这很容易,但有一点需要注意:

要利用“observable collection”功能,需要绑定到它,但
WrapPanel
上没有诸如
ItemsSource
之类的属性

解决方案:

使用
ItemsControl
并将其面板设置为承载项目的
WrapPanel

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="525"
        Height="350"
        mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Button Grid.Row="0"
                Click="Button_Click"
                Content="Add toggle" />
        <ItemsControl Grid.Row="1" ItemsSource="{Binding}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Grid>
</Window>

代码

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        private readonly ObservableCollection<ToggleButton> _collection;

        public MainWindow()
        {
            InitializeComponent();

            _collection = new ObservableCollection<ToggleButton>();

            DataContext = _collection;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var toggleButton = new ToggleButton
            {
                Content = "Toggle" + _collection.Count
            };
            _collection.Add(toggleButton);
        }
    }
}
使用System.Collections.ObjectModel;
使用System.Windows;
使用System.Windows.Controls.Primitives;
命名空间WpfApplication1
{
公共部分类主窗口:窗口
{
私有只读可观察集合\u集合;
公共主窗口()
{
初始化组件();
_集合=新的ObservableCollection();
DataContext=\u集合;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
var toggleButton=新的toggleButton
{
Content=“Toggle”+\u collection.Count
};
_集合。添加(切换按钮);
}
}
}
注意:


将您的收藏分配给
DataContext
可以让您直接处理
WrapPanel
,默认情况下,
绑定到此属性。

这是可能的-看看这个:我想要的样子。但现在很难理解,我的经验很低:)。也许有更多的方法?简单易懂的方法,而且有效。干得好:)
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        private readonly ObservableCollection<ToggleButton> _collection;

        public MainWindow()
        {
            InitializeComponent();

            _collection = new ObservableCollection<ToggleButton>();

            DataContext = _collection;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var toggleButton = new ToggleButton
            {
                Content = "Toggle" + _collection.Count
            };
            _collection.Add(toggleButton);
        }
    }
}