如何以编程方式在wpf c#中更改ContentControl内的UserControl

时间:2015-04-13 04:19:57

标签: c# wpf xaml user-controls

MainWindow.xamlContentControl。 我创建了4 UserControls。 当我在ContentControl内按下按钮时,我想更改MainWindow.xamlUserControl的内容。 这是我的MainWindow.xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:KIOSK" mc:Ignorable="d" x:Class="KIOSK.MainWindow"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <ContentControl Name="contentMain">
        <local:main_screen />
    </ContentControl>
</Window>

这是我的UserControls:

1)main_screen.xaml

<UserControl x:Class="KIOSK.main_screen"
             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="300">
    <Grid Name="grid1" ShowGridLines="True">            
        <Button Margin="10" Background="#FFA4F200" Click="Button_Click"/>                        
    </Grid>
</UserControl>

2)ClubRules.xaml

<UserControl x:Class="KIOSK.ClubRules"
             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="300" Background="White">
    <Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="grid1">            

        <Button Margin="0,15,0,15" Background="#FFFE0555" HorizontalAlignment="Center" Click="Button_Click" />                           
    </Grid>
</UserControl>

在main_creen.xaml.cs里面我写了按下按钮:

ClubRules cr = new ClubRules();
MainWindow mw = new MainWindow();
mw.contentMain.Content = new ClubRules();

但它不起作用.. 我想在按下按钮时更改UserControl内的ContentControl内容。

2 个答案:

答案 0 :(得分:3)

为您的方案使用委托和事件。 发布事件main_screen.xaml.cs并在MainWindow.xaml.cs中订阅该事件

发布活动

<强> main_screen.xaml.cs

public partial class main_screen: UserControl
{
    public Delegate del;
    public main_screen()
    {
        InitializeComponent();
    }
    public void method1()
    {
        del.DynamicInvoke();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        method1();
    }
}

在MainWindow.xaml.cs中订阅该事件

<强> MainWindow.Xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:KIOSK" mc:Ignorable="d" x:Class="KIOSK.MainWindow"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <ContentControl Name="contentMain">
        <local:main_screen x:Name="main_screen_obj" />
    </ContentControl>
</Window>

<强> MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public delegate void ValuePassDelegate();
    public event ValuePassDelegate ValuePassEvent;

    public MainWindow()
    {
        InitializeComponent();
        ValuePassEvent += new ValuePassDelegate(method1);
        main_screen_obj.del = ValuePassEvent;
    }
    public void method1()
    {
        contentMain.Content = new ClubRules();
    }
}

答案 1 :(得分:1)

您正在创建new MainWindow()而不是使用正在显示的那个。您应该将ClubRules分配给正在显示的Content

这样做的一种方法是将按钮从UserControl带到MainWindow本身。 @decoherence建议的其他方法是使用singleton pattern

可能有更多方法,但您基本上需要使用已显示的MainWindow的相同实例。