单击另一页 WPF 上的按钮时选中一页上的复选框

checking the checkbox on one page when click on button on another page WPF

自 1 周以来,我陷入了严重的问题。实际上,我在 MainWindow 页面中有一个按钮 Button1,在用户控件页面中有一个复选框 CheckBox1。所以情况是,当我单击 Button1 时,CheckBox1 应该显示为已检查,但没有发生这样的事情。我在 MainWindow 页面中有 Button1_Click 方法,我通过调用 UserControl class 的实例来调用 CheckBox1.IsChecked = true,例如

UserControl UC = new UserControl();  

UC.CheckBox1.IsChecked =true;  

我正在发布演示代码以进一步说明:这就是我想要做的事情

我的 MainWindow.Xaml 页:

<Window x:Class="MyWpfApplicationDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyWpfApplicationDemo"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <Button x:Name="btnCheck" Width="110" Height="25" Margin="16,10,165,36" FontWeight="Medium" Click="btnCheck_Click" />
        <local:MyUserControlDemo x:Name="MyUserControlDemo" Visibility="Visible" Margin="-54,-49,56,49" />
    </Grid>
</Window>  

我的 MainWindow.Xaml.cs 页面

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            MyUserControlDemo us = new MyUserControlDemo();
            us.chkCheckbox_CheckedChanged(null, null);
        }
    }  

我的 UserControl.Xaml 页面

<UserControl x:Class="MyWpfApplicationDemo.MyUserControlDemo"
             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>

        <CheckBox x:Name="chkCheckbox" Margin="45,0,0,0" Grid.Column="2" Cursor="Hand" Width="100" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="chkCheckbox_CheckedChanged"  />
    </Grid>
</UserControl>    

我的 UserControl.Xaml.cs 页面

public partial class MyUserControlDemo : UserControl
    {
        static int count;
        public MyUserControlDemo()
        {
            InitializeComponent();
        }

        public void chkCheckbox_CheckedChanged(object sender, RoutedEventArgs e)
        {
            chkCheckbox.IsChecked = true;
        }

      }

但是在得到真实值后,复选框没有被选中,我想尽快完成这个,因为我的项目将在解决这个问题后交付。

请帮帮我。
提前致谢

如果您的复选框和您的用户控件在同一个 window 只需转到用户控件的父级即网格(可能),然后转到网格的父级即 MainWindow(可能)并设置chkCheckbox.IsChecked = 真;

示例:

public void chkCheckbox_CheckedChanged(object sender, RoutedEventArgs e)
    {
        var grid = this.Parent as Grid;
       if(grid != null)
       {
           var mainWindow = grid.Parent as MainWindow;
           if(mainWindow != null)
           {
               mainWindow.chkCheckbox.IsChecked = true;
           }
       }     
    }

更新:

<Window x:Class="WpfApplication1.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        x:Name="Main"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:UserControl1 Tag="{Binding ElementName=Main}"/>
        <CheckBox Content="IsChecked?" x:Name="chkBox"/>
    </Grid>
</Window>



private void Button_Click(object sender, RoutedEventArgs e)
        {
            var tag = Tag as MainWindow;
            if (tag != null)
            {
                tag.chkBox.IsChecked = true;
            }
        }

UPD2: 您不需要在按钮单击事件中创建 UserControl 实例,因为您已经在 XAML 中创建了一个实例,因此您需要使用它:

private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            MyUserControlDemo.chkCheckbox.IsChecked = true;
        }

UPD3

这是主窗口 XAML 代码:

<Window x:Class="WpfApplication1.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        x:Name="Main"
        Title="MainWindow" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <local:UserControl1 Tag="{Binding ElementName=Main}"/>
        <local:UserControl2 Grid.Column="1" Tag="{Binding ElementName=Main}" x:Name="Control2"/>
    </Grid>
</Window>

UserControl1:

<UserControl x:Class="WpfApplication1.UserControl1"
             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" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFD3E424" Offset="0"/>
                <GradientStop Color="#FF189724" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
        <Button Width="200" Height="50" Content="CLICK ME" Click="Button_Click"/>
    </Grid>
</UserControl>

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

namespace WpfApplication1
{
    /// <summary>
    /// Логика взаимодействия для UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var tag = Tag as MainWindow;
            if (tag != null)
            {
                tag.Control2.checkBox.IsChecked = true;
            }
        }
    }
}

UserControl2

<UserControl x:Class="WpfApplication1.UserControl2"
             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" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF000CFF" Offset="0"/>
                <GradientStop Color="#FF03FF26" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
        <CheckBox x:Name="checkBox" Content="CHECK ME" Foreground="White" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Center" VerticalContentAlignment="Center"/>
    </Grid>
</UserControl>