我有 MainWindow.xaml
,其中包含 ContentControl
。
我创建了 4 个UserControls
。
当在我的 UserControl
内部按下按钮时,我想更改 MainWindow.xaml
中 ContentControl
的内容。
这是我的 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>
这是我的用户控件:
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 的内容。
最佳答案
针对您的场景使用委托(delegate)和事件。 发布事件 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();
}
}
关于c# - 如何以编程方式更改 wpf c# 中 ContentControl 内部的 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29597773/