我目前有两个用户控件,分别是UC1和UC2。我有一个主窗口。在应用程序启动时,用户可以看到UC1。当用户单击UC1中的按钮时,UC1应该消失并且UC2应该显示在同一窗口中。我尝试了以下方法:

MainWindow.xaml

<Window x:Class="Test.MainWindow"
 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" xmlns:my="clr-namespace:Test" WindowState="Maximized">
<Grid>
    <my:UC1 />
    <my:UC2 Visibility="Collapsed"/>
</Grid>




最初,两个UC都在同一时间可见,因此我使UC2折叠了。现在,我想在单击UC1中的按钮时再次使其可见。我无法从UC1访问UC2的可见性。

UC1.xaml.cs

  private void button2_click(object sender, MouseButtonEventArgs e)
    {
        this.Visibility = System.Windows.Visibility.Collapsed;
        // What to write here?
    }


编辑

对于2个控制器UC1和UC2,使用Tag属性可以正常工作。现在,我添加了另一个控制器UC3,该控制器仅在单击其他按钮后可见。

所以,最后我有了UC1,UC2和UC3,其中只有UC1可见。 UC1有两个按钮,一个单击第一个按钮,只有UC2应该可见,而单击第二个按钮,只有UC3应该可见。

我无法绑定多个标签。有多个标签转换器吗?

最佳答案

编辑:
发布整个代码以使答案更加清晰。而且我也没有任何例外。

MainWindow.xaml:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:h="clr-namespace:WpfApplication4"
        Title="MainWindow"
        Width="525"
        Height="350">
    <Grid>
        <h:UC1 x:Name="Uc1" />
        <h:UC2 x:Name="Uc2" Visibility="Hidden" />
    </Grid>
</Window>


MainWindow.xaml.cs:

using System.Windows;

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}


UC1.xaml:

<UserControl x:Class="WpfApplication4.UC1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <Grid>
        <Ellipse Width="100"
                 Height="100"
                 Margin="65,113,0,0"
                 HorizontalAlignment="Left"
                 VerticalAlignment="Top"
                 Fill="CornflowerBlue"
                 Stroke="Black" />
        <Button Width="75"
                Margin="177,239,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Click="ButtonBase_OnClick"
                Content="ABCD" />
    </Grid>
</UserControl>


UC1.xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for UC_.xaml
    /// </summary>
    public partial class UC1 : UserControl
    {
        public UC1()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Window parentWindow = Application.Current.MainWindow;
            if (parentWindow.GetType() == typeof(MainWindow))
            {
                (parentWindow as MainWindow).Uc1.Visibility = Visibility.Collapsed;
                (parentWindow as MainWindow).Uc2.Visibility = Visibility.Visible;
            }
        }
    }
}


UC2.xaml:

<UserControl x:Class="WpfApplication4.UC2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <Grid>
        <Ellipse Width="100"
                 Height="100"
                 Margin="169,22,0,0"
                 HorizontalAlignment="Left"
                 VerticalAlignment="Top"
                 Fill="Orange"
                 Stroke="Black" />
    </Grid>
</UserControl>


UC2.xaml.cs:

using System.Windows.Controls;

namespace WpfApplication4
{
    /// <summary>
    /// Interaction logic for UC2.xaml
    /// </summary>
    public partial class UC2 : UserControl
    {
        public UC2()
        {
            InitializeComponent();
        }
    }
}

关于c# - 在当前用户控件中单击按钮时显示不同的用户控件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38088220/

10-11 13:41