我创建了下一个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 ++语法。和信号槽方法更接近我:)
你对我的建议是什么?
答案 0 :(得分:0)
您可以使用RoutedEvent's behavior。您只需将Window注册到Click RoutedEvent即可。看看:
var supplier = this
然后,您不需要在UserControls中使用按钮单击事件处理程序。
我希望这个样本可以帮到你。