WPF-集合之按钮排布

使用视图模型和数据绑定来实现按分类显示按钮的功能。以下是一个简单的示例,演示如何在WPF中点击按钮以按分类显示不同按钮。

首先,你需要一个视图模型,其中包含按钮数据和当前选定的分类。使用MVVM框架(如MVVM Light或Stylet)来管理视图模型。这是一个示例:

1.View层:首先实现按钮的横向排布

功能1:

    <Grid>
        <Button Content="Button 1" HorizontalAlignment="Left" Margin="10,10,0,1016" Command="{Binding ShowCategory1Command}" Grid.Row="0"/>
        <Button Content="Button 2" HorizontalAlignment="Left" Margin="92,10,0,1016" Command="{Binding ShowCategory2Command}" Grid.Row="0"/>
        <Button Content="Button 3" HorizontalAlignment="Left" Margin="165,10,0,1016" Command="{Binding ShowCategory3Command}" Grid.Row="0"/>
        <UniformGrid Background="#FF919CF7" Margin="619,14,285,809">
            <!-- 用于生成按钮的 ItemsControl -->
            <ItemsControl ItemsSource="{Binding DisplayedButtons}" BorderThickness="0">
                <!-- 设置 ItemsControl 的 ItemsPanel 为 VirtualizingStackPanel,按水平方向排列 -->
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <!-- 设置每个按钮的样式,数据绑定等 -->
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding ButtonText}"
                        Command="{Binding DataContext.ButtonClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                        Margin="5"
                        Width="100"
                        Height="30"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </UniformGrid>
    </Grid>

功能二:希望使用 UniformGrid 嵌套在 ItemsControl 内部以生成四行四列的按钮,可以使用以下

代码:

    <Grid>
        <Button Content="Button 1" HorizontalAlignment="Left" Margin="10,10,0,1016" Command="{Binding ShowCategory1Command}" Grid.Row="0"/>
        <Button Content="Button 2" HorizontalAlignment="Left" Margin="92,10,0,1016" Command="{Binding ShowCategory2Command}" Grid.Row="0"/>
        <Button Content="Button 3" HorizontalAlignment="Left" Margin="165,10,0,1016" Command="{Binding ShowCategory3Command}" Grid.Row="0"/>
        <UniformGrid Rows="4" Columns="4" Background="#FF919CF7" Margin="619,14,285,809">
            <!-- 用于生成四行四列的按钮的 ItemsControl -->
            <ItemsControl ItemsSource="{Binding DisplayedButtons}" BorderThickness="0" Margin="0,0,-633,-194">
                <!-- 设置 ItemsControl 的 ItemsPanel 为 UniformGrid,确保四行四列的按钮网格 -->
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Rows="4" Columns="4"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <!-- 设置每个按钮的样式,数据绑定等 -->
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding ButtonText}"
                        Command="{Binding DataContext.ButtonClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                        Margin="5"
                        Width="100"
                        Height="30"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </UniformGrid>

    </Grid>

2.ViewModel层:

在这个示例中,CeshiModel 表示按钮的数据模型,包括按钮文本和所属的分类。CeshiViewModel 包括一个 DisplayedButtons 属性,用于存储当前显示的按钮,以及三个命令 ShowCategory1CommandShowCategory2CommandShowCategory3Command分别用于显示不同的分类按钮。

using ExperimentalPlatforms.Models;
using GalaSoft.MvvmLight.Command;
using Stylet;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;

namespace ExperimentalPlatforms.ViewModels.Windows
{
    public  class CeshiViewModel:Screen
    {
        private ObservableCollection<CeshiModel> allButtons;
        private ObservableCollection<CeshiModel> displayedButtons;
        private string selectedCategory;
        public ICommand ShowCategory1Command { get; private set; }
        public ICommand ShowCategory2Command { get; private set; }
        public ICommand ShowCategory3Command { get; private set; }
        public CeshiViewModel()
        {
            // 初始化按钮数据(这里你可以从数据源加载按钮数据)
            allButtons = new ObservableCollection<CeshiModel>
            {
                new CeshiModel { ButtonText = "Button 1", Category = "Category 1" },
                new CeshiModel { ButtonText = "Button 2", Category = "Category 2" },
                new CeshiModel { ButtonText = "Button 3", Category = "Category 3" },
                new CeshiModel { ButtonText = "Button 3", Category = "Category 3" },
                 new CeshiModel { ButtonText = "Button 3", Category = "Category 3" },
                 new CeshiModel { ButtonText = "Button 3", Category = "Category 3" },
                  new CeshiModel { ButtonText = "Button 3", Category = "Category 3" },
                 new CeshiModel { ButtonText = "Button 3", Category = "Category 3" },
                 new CeshiModel { ButtonText = "Button 3", Category = "Category 3" },
                // 添加更多按钮
            };

            // 初始状态下显示所有按钮
            displayedButtons = new ObservableCollection<CeshiModel>(allButtons);

            // 初始化命令
            ShowCategory1Command = new RelayCommand(ShowCategory1);
            ShowCategory2Command = new RelayCommand(ShowCategory2);
            ShowCategory3Command = new RelayCommand(ShowCategory3);
        }

        public ObservableCollection<CeshiModel> DisplayedButtons
        {
            get { return displayedButtons; }
            set
            {
                if (displayedButtons != value)
                {
                    displayedButtons = value;
                    OnPropertyChanged(nameof(DisplayedButtons)); // 触发属性更改通知
                }
            }
        }
        private void ShowCategory1()
        {
            // 仅显示Category 1 的按钮
            DisplayedButtons = new ObservableCollection<CeshiModel>(allButtons.Where(b => b.Category == "Category 1"));
        }

        private void ShowCategory2()
        {
            // 仅显示Category 2 的按钮
            DisplayedButtons = new ObservableCollection<CeshiModel>(allButtons.Where(b => b.Category == "Category 2"));
        }
        private void ShowCategory3()
        {
            // 仅显示Category 2 的按钮
            DisplayedButtons = new ObservableCollection<CeshiModel>(allButtons.Where(b => b.Category == "Category 3"));
        }
    }
}

3.Model层:

CeshiModel是包含按钮数据模型的集合。每个按钮数据模型包括Category ButtonText属性,用于区分按钮的类别和文本。

  public  class CeshiModel
    {
        public string ButtonText { get; set; }
         public string Category { get; set; }  //类别
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值