欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c# usercontrol

程序员文章站 2022-06-08 18:35:33
...

需要做一个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>