如何将 ObservableCollection<T> 绑定到 WrapPanel?
How to bind an ObservableCollection<T> to a WrapPanel?
我正在使用 WPF。我有 ObservableCollection
个 ToggleButton
:
private ObservableCollection<ToggleButton> myg = new ObservableCollection<ToggleButton>();
我想将这些 ObservableCollection
控件 (ToggleButtons
) 绑定为 WrapPanel
子项。每次我使用 myg.Add(new ToggleButton)
时,我都希望它自动将控件添加到 WrapPanel
。
示例XAML:
<WrapPanel Name="test1">
<!-- I want to bind (add) these controls here -->
</WrapPanel>
Is it possible, if yes - how ? Maybe there is other similar way to do that ?
这很容易,但有一个小问题:
要利用 '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);
}
}
}
注:
将您的 collection 分配给 DataContext
可以让您直接处理 WrapPanel
,<ItemsControl Grid.Row="1" ItemsSource="{Binding}">
默认绑定到此 属性。
我正在使用 WPF。我有 ObservableCollection
个 ToggleButton
:
private ObservableCollection<ToggleButton> myg = new ObservableCollection<ToggleButton>();
我想将这些 ObservableCollection
控件 (ToggleButtons
) 绑定为 WrapPanel
子项。每次我使用 myg.Add(new ToggleButton)
时,我都希望它自动将控件添加到 WrapPanel
。
示例XAML:
<WrapPanel Name="test1">
<!-- I want to bind (add) these controls here -->
</WrapPanel>
Is it possible, if yes - how ? Maybe there is other similar way to do that ?
这很容易,但有一个小问题:
要利用 '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);
}
}
}
注:
将您的 collection 分配给 DataContext
可以让您直接处理 WrapPanel
,<ItemsControl Grid.Row="1" ItemsSource="{Binding}">
默认绑定到此 属性。