c# usercontrol

unity3d 专栏收录该内容
27 篇文章 1 订阅

需要做一个tab切换,但又不想一个页面加载太多数据,又因为大框架下使用了Frame,所以也不能用Frame了,就用button结合usercontrol做了一个tab切换

先新建wpf用户控件,如AdminMain.xaml

<StackPanel Name="topTabPanel" Height="48" Margin="0,0,0,20"></StackPanel>

 

后台使用了button生成

public partial class AdminMain : UserControl
    {
        private Dictionary<string, Uri> allViews = new Dictionary<string, Uri>();
        public AdminMain()
        {
            InitializeComponent();
            CreateTopMenu();
            allViews.Add("Formula", new Uri("Views/FormulaList.xaml", UriKind.Relative));
            allViews.Add("DeviceType", new Uri("Views/DeviceTypeList.xaml", UriKind.Relative));
            allViews.Add("Device", new Uri("Views/DeviceList.xaml", UriKind.Relative));
            allViews.Add("User", new Uri("Views/UserList.xaml", UriKind.Relative));
            allViews.Add("Notice", new Uri("Views/NoticeList.xaml", UriKind.Relative));
            allViews.Add("Feedback", new Uri("Views/FeedbackList.xaml", UriKind.Relative));
        }
        private void CreateTopMenu()
        {

            Grid grid = CreateMenuGrid("formula");
            this.topTabPanel.Children.Add(grid);
            StackPanel.SetZIndex(grid, 0);
        }

        public Grid CreateMenuGrid(string currentMenu)
        {
            Grid grid = new Grid();
            grid.Width = 420;
            grid.HorizontalAlignment = HorizontalAlignment.Left;

            // 实例化1行
            RowDefinition row = new RowDefinition();
            bool is_current = false;
            //实例化4列
            ColumnDefinition formula = new ColumnDefinition();
            is_current = currentMenu == "formula" ? true : false;
            Button formulaBtn = CreateButton("配方管理", this.FormulaManageClick, is_current);

            ColumnDefinition deviceType = new ColumnDefinition();
            is_current = currentMenu == "deviceType" ? true : false;
            Button deviceTypeBtn = CreateButton("设备类型", this.DeviceTypeManageClick, is_current);

            ColumnDefinition device = new ColumnDefinition();
            is_current = currentMenu == "device" ? true : false;
            Button deviceBtn = CreateButton("设备管理", this.DeviceManageClick, is_current);

            ColumnDefinition user = new ColumnDefinition();
            is_current = currentMenu == "user" ? true : false;
            Button userBtn = CreateButton("员工管理", this.UserManageClick, is_current);

            ColumnDefinition notice = new ColumnDefinition();
            is_current = currentMenu == "notice" ? true : false;
            Button noticeBtn = CreateButton("通知管理", this.NoticeManageClick, is_current);

            ColumnDefinition feedback = new ColumnDefinition();
            is_current = currentMenu == "notice" ? true : false;
            Button feedbackBtn = CreateButton("留言管理", this.FeedbackManageClick, is_current);


            grid.RowDefinitions.Add(row);
            grid.ColumnDefinitions.Add(formula);
            grid.ColumnDefinitions.Add(deviceType);
            grid.ColumnDefinitions.Add(device);
            grid.ColumnDefinitions.Add(user);
            grid.ColumnDefinitions.Add(notice);
            grid.ColumnDefinitions.Add(feedback);

            Grid.SetColumn(formulaBtn, 0);
            Grid.SetColumn(deviceTypeBtn, 1);
            Grid.SetColumn(deviceBtn, 2);
            Grid.SetColumn(userBtn, 3);
            Grid.SetColumn(noticeBtn, 4);
            Grid.SetColumn(feedbackBtn, 5);

            grid.Children.Add(formulaBtn);
            grid.Children.Add(deviceTypeBtn);
            grid.Children.Add(deviceBtn);
            grid.Children.Add(userBtn);
            grid.Children.Add(noticeBtn);
            grid.Children.Add(feedbackBtn);

            return grid;
        }

        private Button CreateButton(string content, RoutedEventHandler re, bool is_current)
        {
            Button btn = new Button();
            btn.Content = content;
            btn.Padding = new Thickness(20, 10, 20, 10);
            if (is_current)
            {
                btn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#033a6b"));
            }
            else
            {
                btn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#005bac"));
                btn.Click += new RoutedEventHandler(re);
            }
            btn.Foreground = new SolidColorBrush(Colors.White);
            return btn;
        }

        private void FormulaManageClick(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("Views/FormulaList.xaml", UriKind.Relative));
        }

        private void DeviceTypeManageClick(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("Views/DeviceTypeList.xaml", UriKind.Relative));
        }

        private void DeviceManageClick(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("Views/DeviceList.xaml", UriKind.Relative));
        }

        private void UserManageClick(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("Views/UserList.xaml", UriKind.Relative));
        }

        private void NoticeManageClick(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("Views/NoticeList.xaml", UriKind.Relative));
        }

        private void FeedbackManageClick(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("Views/FeedbackList.xaml", UriKind.Relative));
        }

 

 

使用

新建页面,比如UserList.xaml

然后加入代码到xmal布局页面

<local:AdminMain></local:AdminMain>
<StackPanel>
        <local:AdminMain></local:AdminMain>
        <Grid Width="160" HorizontalAlignment="Left" Margin="20">
            <Button Name="userAct" Style="{StaticResource AddButton}" Click="UserAddClick">
                <StackPanel  Orientation="Horizontal">
                    <TextBlock Style="{StaticResource FontAwesome}" Text="{StaticResource fa-plus}" Width="28" Height="28" FontSize="28" />
                    <TextBlock Text="添加" Height="28" LineHeight="28" FontSize="18" />
                </StackPanel>
            </Button>
        </Grid>
        <Grid>
            <DataGrid Name="userList"  AutoGenerateColumns="False" CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="用户名" Width="800" Binding="{Binding name}" />
                    <DataGridTextColumn Header="工 号" Binding="{Binding user_sn}" />
                    <DataGridTextColumn Header="时 间" Binding="{Binding created_at}" />
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Button Content="编 辑"  Tag="{Binding id}" Click="UserEditClick" />
                                    <Button Content="删 除" Tag="{Binding id}" Click="UserDeleteClick" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </StackPanel>

 

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页

打赏作者

tang05709

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值