在WPF中向表格排列动态添加按钮
我想做一个2列的表格安排,并在运行时向表格中添加按钮 我所做的是定义与此类似的嵌套StackPanel在WPF中向表格排列动态添加按钮,wpf,stackpanel,Wpf,Stackpanel,我想做一个2列的表格安排,并在运行时向表格中添加按钮 我所做的是定义与此类似的嵌套StackPanel <StackPanel MinWidth="500" MaxWidth="800" MaxHeight="400" HorizontalAlignment="Center"> <TextBlock HorizontalAlignment="Center" Foreground="Black" Margin="0,0,0,5" FontS
<StackPanel MinWidth="500" MaxWidth="800" MaxHeight="400" HorizontalAlignment="Center">
<TextBlock HorizontalAlignment="Center" Foreground="Black" Margin="0,0,0,5" FontSize="20">Some Title</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="" MinWidth="100" MinHeight="100" Margin="10,0,0,10"></Button>
<Button Content="" MinWidth="100" MinHeight="100" Margin="10,0,0,10"></Button>
</StackPanel>
</StackPanel>
一些头衔
这是一个正确的开始,还是有一个更好更简单的安排?您提到要动态地使用它,您不想使用
StackPanel
,因为您最终会保持其内部控件的宽度和高度。如果您希望控件自动调整其自身大小,请执行以下操作
最好的解决方案是使用网格
,如果您真的想进行两列表格排列的话
看看这个
示例代码
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="28" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
您可以将模型视图实现与ListView一起使用,并将其绑定到具有按钮相应处理程序的项目集合:
WPF:
MyItem和MyItemWrapper
public class MyItem
{
public string Name { get; set; }
public object Data { get; set; }
}
public class MyItemWrapper
{
private MyItem _item;
public MyItemWrapper(MyItem item)
{
_item = item;
ButtonPress = new DelegateCommand<object>(OnButtonPress);
}
public string Name
{
get { return _item.Name; }
}
public DelegateCommand<object> ButtonPress { get; private set; }
private void OnButtonPress(object args)
{
System.Diagnostics.Debug.WriteLine("Button pressed for: " + Name);
}
}
公共类MyItem
{
公共字符串名称{get;set;}
公共对象数据{get;set;}
}
公共类MyItemWrapper
{
私有MyItem _项;
公共MyItemWrapper(MyItem项目)
{
_项目=项目;
ButtonPress=新的DelegateCommand(OnButtonPress);
}
公共字符串名
{
获取{return\u item.Name;}
}
public DelegateCommand按钮按{get;private set;}
私有void OnButtonPress(对象参数)
{
System.Diagnostics.Debug.WriteLine(“按下“+Name”按钮);
}
}
这将最终能够通过在视图模型中使用MyItems在运行时添加/删除项目,并使您的列表视图始终自动更新。我使用WrapPanel实现了这一功能,我将分享我所做的工作,感谢Vlad的绑定部分:
<ListBox ItemsSource="{Binding CartItemC.CartItems}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel x:Name="wrapPanel" MaxWidth="300" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel MaxWidth="300">
<Button Command="{Binding ButtonPress}">Click me</Button>
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
点击我
public partial class MyView : UserControl
{
public MyView(MyViewModel viewModel)
{
DataContext = viewModel;
InitializeComponents()
}
}
public class MyItem
{
public string Name { get; set; }
public object Data { get; set; }
}
public class MyItemWrapper
{
private MyItem _item;
public MyItemWrapper(MyItem item)
{
_item = item;
ButtonPress = new DelegateCommand<object>(OnButtonPress);
}
public string Name
{
get { return _item.Name; }
}
public DelegateCommand<object> ButtonPress { get; private set; }
private void OnButtonPress(object args)
{
System.Diagnostics.Debug.WriteLine("Button pressed for: " + Name);
}
}
<ListBox ItemsSource="{Binding CartItemC.CartItems}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel x:Name="wrapPanel" MaxWidth="300" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel MaxWidth="300">
<Button Command="{Binding ButtonPress}">Click me</Button>
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>