【问题标题】:checking the checkbox on one page when click on button on another page WPF单击另一页WPF上的按钮时检查一页上的复选框
【发布时间】:2016-09-06 20:19:05
【问题描述】:

自 1 周以来,我就遇到了严重的问题。实际上,我在MainWindow 页面中有一个按钮,比如Button1,在用户控制页面中有一个复选框,比如CheckBox1。所以情况是,当我点击Button1 时,CheckBox1 应该显示为 Checked 但没有发生这样的事情。我在 MainWindow 页面中有 Button1_Click 方法,我通过调用 UserControl 类的实例来调用 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;
        }

      }

但在获得真实值后,未选中该复选框,我希望尽快完成此操作,因为我的项目将在解决此问题后交付。

请帮帮我。
提前致谢

【问题讨论】:

  • 窗口上是否添加了用户控件?你能发布更多代码更具体吗?
  • 是的..我在窗口上添加了用户控件
  • 请发布更多代码以进行澄清。贴出代码还不够
  • 您的问题和建议答案的 cmets 信息不足以了解您的实际需求。请提供更多代码/更好地解释您的问题

标签: wpf wpf-controls


【解决方案1】:

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

示例:

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

这是 MainWindow 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>

用户控制1:

<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;
            }
        }
    }
}

用户控件2

<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>

【讨论】:

  • 我将网格值设为空。在 mainWindow.chkCheckbox 上也出现红色错误
  • 然后将您的窗口绑定到 userControl 上的标记。请参阅答案中的示例。
  • @Ammy 检查答案
  • 不,我不想在主窗口页面上使用复选框控件..我的要求不同..无论如何我想在用户控件页面上使用复选框..
  • @ammy 检查答案更新,不要在点击事件中创建 MyUserControlDemo,在 XAML 中创建 MyUserControlDemo 的用户名!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
相关资源
最近更新 更多