如何将控制从一个控件转移到另一个控件

时间:2015-05-14 11:37:51

标签: c# wpf

我创建了下一个WPF控件:Window1,View,Edit in different files:

在MainWindow.xaml中MainWindow发送两个ContentControls

    <Grid>
    <ContentControl x:Name="Viewer" Content="ContentControl1" DockPanel.Dock="Top" Height="Auto" RenderTransformOrigin="0.5,0.5">
    </ContentControl>
    <ContentControl x:Name="Editor" Content="ContentControl2" DockPanel.Dock="Top" Height="Auto" VerticalAlignment="Bottom" RenderTransformOrigin="0.5,0.5" Visibility="Hidden"/>
</Grid>

这是 .cs 文件的一部分:

public partial class MainWindow : Window
    {
        View v = new View();
        Edit e = new Edit();

        public MainWindow()
        {
            InitializeComponent();

            Window1.Viewer.Content = v;
            Window1.Editor.Content = e;
        }
        public  void swichToEditor()
        {
            Window1.Editor.Visibility = System.Windows.Visibility.Visible;
            Window1.Viewer.Visibility = System.Windows.Visibility.Hidden;
        }

        public void swichToViewer()
        {
            Window1.Ediort.Visibility = System.Windows.Visibility.Hidden;
            Window1.Viewer.Visibility = System.Windows.Visibility.Visible;

        }
    }
带有ComtentControl1

x:name=Editor包含此userControl:

    public partial class Edit : UserControl
        {
            public Edit()
            {
                this.InitializeComponent();

            }

            private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                // TODO: Add event handler implementation here.

            }
        }
带有ComtentControl2

x:name=Viewer包含此userControl:

    public partial class View : UserControl
        {
            public View()
            {
                this.InitializeComponent();
            }

            private void Button2_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                // TODO: Add event handler implementation here.
            }
        }

button1被按下时,我需要隐藏Editor并显示Viewer。反之亦然 我知道更好的c ++语法。和信号槽方法更接近我:) 你对我的建议是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用RoutedEvent's behavior。您只需将Window注册到Click RoutedEvent即可。看看:

var supplier = this

然后,您不需要在UserControls中使用按钮单击事件处理程序。

我希望这个样本可以帮到你。