从主网格中的另一个UserControl打开新的UserControl

时间:2018-06-01 03:26:38

标签: c# wpf listview

当我点击UserControl3中的按钮时,我想在主网格中打开我的UserControl4。每次我单击按钮时它会快速显示index = 0中的UserForm4并在我的情况下自动转到控件(如果我将index = 1再次显示UserControl3)。

我尝试从.xaml代码中删除selectionIndex并在登录后设置索引,但它没有工作。

.xaml上的代码

<materialDesign:TransitioningContent x:Name="TransitioningContentSlide" OpeningEffect="{materialDesign:TransitionEffect SlideInFromLeft, Duration=0:0:0.2}">
            <Grid x:Name="GridCursor"  Margin="0 235 0 100" Background="#FF0069C0" Width="10" HorizontalAlignment="Left" Height="60" VerticalAlignment="Top"/>
        </materialDesign:TransitioningContent>
        <ListView x:Name="ListViewMenu" Margin="0,235,0,100" FontSize="18" SelectionChanged="ListViewMenu_SelectionChanged" SelectedIndex="0">
            <ListViewItem Height="60" Foreground="LightGray">
                <StackPanel Orientation="Horizontal">
                    <materialDesign:PackIcon Kind="Home" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
                    <TextBlock Text="Início" FontSize="22" VerticalAlignment="Center" Margin="20 0"/>
                </StackPanel>
            </ListViewItem>
            <ListViewItem Height="60" Foreground="LightGray">
                <StackPanel Orientation="Horizontal">
                    <materialDesign:PackIcon Kind="GamepadVariant" Width="30" Height="30" VerticalAlignment="Center" Margin="5"/>
                    <TextBlock Text="Jogos" FontSize="22" VerticalAlignment="Center" Margin="20 0"/>
                </StackPanel>
            </ListViewItem>

xaml.cs上的代码

private void ListViewMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                int index = ListViewMenu.SelectedIndex;
                MoveCursorMenu(index);

                switch (index)
                {
                    case 0:
                        GridPrincipal.Children.Clear();
                        GridPrincipal.Children.Add(new UserControl1());
                        break;
                    case 1:
                        GridPrincipal.Children.Clear();
                        GridPrincipal.Children.Add(new UserControl3());
                        break;
                    default:
                        break;
                }
            }

            private void MoveCursorMenu(int index)
            {
                TransitioningContentSlide.OnApplyTemplate();
                GridCursor.Margin = new Thickness(0, (235 + (60 * index)), 0, 0);
            }

我的UserForm3上的代码

private void button_Click(object sender, RoutedEventArgs e)
        {
            Window3 w3 = new Window3();
            w3.GridPrincipal.Children.Clear();
            w3.GridPrincipal.Children.Add(new UserControl4());
            w3.Show();

            w3.ListViewMenu.SelectedIndex = 1;


        }

0 个答案:

没有答案