WPF clicks buttons on different interfaces to achieve interface switching

All I see on the Internet are using the same button on an interface to achieve interface switching. Here I will make different buttons on different interfaces to achieve interface switching. As shown below:

You can see that there is a "Query Data" button in the lower right corner. When you click the query button, you will switch to another interface, as shown below:

Although they look alike, they are by no means the same button. When you click to return to the query, you will return to the previous interface.

code show as below:

The first interface is called DataManage, and its xmal:

<UserControl x:Class="MyControlLibrarys.DataManage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"  Margin="10 0"
              d:DesignHeight="600" d:DesignWidth="850" Loaded="UserControl_Loaded">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/GeneralStyle;component/Themes/Generic.xaml" />
                <ResourceDictionary Source="pack://application:,,,/GeneralStyle;component/Themes/DataGrid.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="120"/>
        </Grid.RowDefinitions>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="3*"/>
                    <ColumnDefinition Width="4*"/>
                    <ColumnDefinition Width="4*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="4*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="4*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
            <Label Content="查询条件" Grid.Column="0" FontSize="20" VerticalContentAlignment="Bottom"  HorizontalContentAlignment="Right"/>
            <CheckBox Grid.Row="1" Grid.Column="0" Name="uut_nameChebox" Content="UUT类型" FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
            <ComboBox Grid.Row="1" Grid.Column="1" Name="uut_nameCombox" FontSize="14" Margin="10" Background="Transparent" DropDownClosed="Combox_DropDownClosed"/>
            <CheckBox Grid.Row="2" Grid.Column="0" Name="uut_kindChebox" Content="UUT型号" FontSize="16"  HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
            <ComboBox Grid.Row="2" Grid.Column="1" Name="uut_kindCombox" FontSize="14" Background="Transparent" Margin="10" DropDownClosed="Combox_DropDownClosed"/>
            <CheckBox Grid.Row="1" Grid.Column="2" Name="timeChebox" Content="操作时间" FontSize="16"  HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
            <Label Content="从" Grid.Row="1" Grid.Column="3" FontSize="16" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"></Label>
            <DatePicker Grid.Row="1" Grid.Column="4" Name="timeStatCombox" FontSize="14" Background="Transparent" Margin="0 10" CalendarClosed="DatePicker_CalendarClosed"/>
            <Label Content="到" Grid.Row="1" Grid.Column="5" FontSize="16" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"></Label>
            <DatePicker Grid.Row="1" Grid.Column="6" Name="timeEndCombox" FontSize="14" Background="Transparent" Margin="0 10" CalendarClosed="DatePicker_CalendarClosed"/>
            <CheckBox Grid.Row="2" Grid.Column="2" Name="userChebox" Content="操 作 员 " FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
            <ComboBox Grid.Row="2" Grid.Column="4" Name="userCombox" FontSize="14" Background="Transparent" Margin="0 10" DropDownClosed="Combox_DropDownClosed"/>
            <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="5" Grid.ColumnSpan="4" HorizontalAlignment="Center">
                <Button Content="重置" Style="{DynamicResource TeamviwerButtonStyle}" Width="100" Height="35" Margin="20 0" Click="Reset_Click"/>
                <Button Content="查询" Style="{DynamicResource TeamviwerButtonStyle}" Width="100" Height="35" Margin="10 0" Click="ConditionQuery_Click"/>
            </StackPanel>
            <Separator Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="9" Margin="20 5 20 20"/>
        </Grid>
        <DataGrid Grid.Row="1" ColumnHeaderStyle ="{DynamicResource DataGridColumnHeaderStyle}" Name="dgData"  AutoGenerateColumns="False" CanUserAddRows="False"
                  GridLinesVisibility="All" MouseDoubleClick="dgData_MouseDoubleClick">
            <DataGrid.Columns>
                <DataGridTextColumn Width="0.5*" Header="序号" ElementStyle="{StaticResource contentCenterStyle}" Binding="{Binding Path=OrderId, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                <DataGridTextColumn Header="测试编号" Width="*" Binding="{Binding Path=id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden"></DataGridTextColumn>
                <DataGridTextColumn Header="UUT" Width="*" Binding="{Binding Path=uut_name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
                <DataGridTextColumn Header="UUT型号" Width="*" Binding="{Binding Path=uut_kind, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
                <DataGridTextColumn Header="UUT编号" Width="*" Binding="{Binding Path=uut_num, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
                <DataGridTextColumn Header="操作员" Width="*" Binding="{Binding Path=user, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
                <DataGridTextColumn Header="测试时间" Width="2*" Binding="{Binding Path=time,StringFormat='{}{0:yyy-MM-dd HH:mm:ss}', Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                <DataGridTextColumn Header="备注" Width="*" Binding="{Binding Path=remark, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <StackPanel Grid.Row="2" Name="ClipCtrlStackPanel" Margin="0 0 0 20"></StackPanel>
        <StackPanel Grid.Row="3"  Orientation="Horizontal"  HorizontalAlignment="Right">
            <Button Content="查看数据" Style="{DynamicResource TeamviwerButtonStyle}" Width="120" Height="40" Margin="20 0" Click="CatData_Click"/>
            <Button Content="删除数据" Style="{DynamicResource TeamviwerButtonStyle}" Width="120" Height="40" Margin="20 0" Click="DelData_Click"/>
        </StackPanel>
    </Grid>
</UserControl>

Background code:

Define a delegate under the namespace:

public delegate void CtrlSwitchHandler(); //Define the delegate

Then, define the event, and trigger the event when you click to view the data:

public event CtrlSwitchHandler CtrlSwitchEvent; //Define events for interface switching
/// <summary>
        /// View data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CatData_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgData.SelectedItem == null)
            {
                MyDialog md = new MyDialog(1, "The object to be viewed is not selected, please select!");
                md.Show();
            }
            else
            {
                Result_NumStr = ((DataRowView)this.dgData.SelectedItem).Row["id"].ToString();
                if(Result_NumStr != "")
                {
                    //Trigger event to change the value of parent window
                    if (CtrlSwitchEvent != null)
                    {
                        CtrlSwitchEvent();
                    }
                }
                else
                {
                    MyDialog md = new MyDialog(1, "Null values ​​are not allowed!");
                    md.Show();
                }
            }
        }

The return query principle is the same, my interface here is called CatDataControl, which is useful below, but don't define the delegate anymore, so I won't describe it here.

Finally, use a UserControl to load these two controls, or you can directly use the mainwindow to load them. I need the project here, so I use the UserControl control to implement the switching code as follows:

xaml:

<UserControl x:Class="MyControlLibrarys.SwitchInterface"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="350" Loaded="SwInterface_Load">
    <Grid>
        <StackPanel Name="stackPanelData"></StackPanel>
    </Grid>
</UserControl>

Background code:

public partial class SwitchInterface : UserControl
    {
        public SwitchInterface()
        {
            InitializeComponent();
        }

        DataManage dataManage = new DataManage(); //User controls for switching -- data management controls
        private double originalHeight = 0.0;
        private void SwInterface_Load(object sender, RoutedEventArgs e)
        {
            originalHeight = stackPanelData.ActualHeight;
            dataManage.CtrlSwitchEvent += new CtrlSwitchHandler(CtrlSwitch_CatData);
            LoadStackPanelRight (dataManage);
        }

        / / Implement the interface switch to the view data interface
        private void CtrlSwitch_CatData()
        {
            CatDataControl catData = new CatDataControl(dataManage.Result_NumStr); //User control for switching -- view data control
            catData.CtrlSwitchEvent += new CtrlSwitchHandler(CtrlSwitch_dataManage);
            LoadStackPanelRight(catData);
        }

        / / Switch the interface to the data management interface
        private void CtrlSwitch_dataManage()
        {
            LoadStackPanelRight (dataManage);
        }

        //Load user control function function
        private void LoadStackPanelRight(UserControl userControl)
        {
            stackPanelData.Children.Clear();
            stackPanelData.Children.Add(userControl);
        }
    }
如此即可实现,这里面的代码不是每个都有用,这只是部分代码,但绝对能实现界面切换。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325724744&siteId=291194637