我从1周起就陷入了深刻的问题。实际上,我在MainWindow
页面中有一个按钮说Button1
,而在Usercontrol页面中有一个复选框说CheckBox1
。所以情况是,当我点击Button1
时,CheckBox1
应该显示为已检查但没有发生这样的事情。我在MainWindow页面中有Button1_Click方法,我通过调用UserControl类的实例来调用CheckBox1.IsChecked = true
,如
UserControl UC = new UserControl();
UC.CheckBox1.IsChecked =true;
我发布了一个演示代码以获得更多说明:这是我想做的事情
My MainWindow.Xaml Page:
<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;
}
}
但在获得真值后,未选中复选框,我希望尽快完成此操作,因为我的项目将在解决此问题后交付。
请帮帮我 提前致谢
答案 0 :(得分:0)
如果你的复选框和你的用户控件在同一个窗口中,只需转到网格(可能)的用户控件的父级,然后转到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;
}
}
}
UPD:
<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;
}
这里是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>
<强>的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>