我有一些代码可以更改按钮点击功能本身中的按钮颜色,效果很好。
_5.Background = new SolidColorBrush(Color.FromArgb(50, 180, 20, 30));
但是当我尝试使用对象引用从另一种形式调用_5时,尽管编译正确,也没有任何反应。
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MainWindow b = new MainWindow();
b._5.Background = new SolidColorBrush(Color.FromArgb(50, 180, 20, 30));
}
有人可以指出我做错了什么吗?
谢谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SBI_Mgt_System
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void _1_Click(object sender, RoutedEventArgs e)
{
Room1 openRoom = new Room1();
openRoom.Show();
rooms x = new rooms();
x.Show();
}
public void Button_Click(object sender, RoutedEventArgs e)
{
Room1 openRoom = new Room1();
openRoom.Show();
rooms x = new rooms();
x.Show();
}
public void Button_Click_1(object sender, RoutedEventArgs e)
{
Room1 openRoom = new Room1();
openRoom.Show();
rooms x = new rooms();
x.Show();
}
public void Button_Click_2(object sender, RoutedEventArgs e)
{
Room1 openRoom = new Room1();
openRoom.Show();
rooms x = new rooms();
x.Show();
}
public void Button_Click_3(object sender, RoutedEventArgs e)
{
Room1 openRoom = new Room1();
openRoom.Show();
rooms x = new rooms();
x.Show();
}
public void Button_Click_4(object sender, RoutedEventArgs e)
{
Room1 openRoom = new Room1();
openRoom.Show();
rooms x = new rooms();
x.Show();
}
public void Button_Click_5(object sender, RoutedEventArgs e)
{
Room1 openRoom = new Room1();
openRoom.Show();
rooms x = new rooms();
x.Show();
}
}
}
另一种形式...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace SBI_Mgt_System
{
/// <summary>
/// Interaction logic for rooms.xaml
/// </summary>
public partial class rooms : Window
{
public static bool res = false;
public rooms()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MainWindow b = new MainWindow();
b._5.Background = new SolidColorBrush(Color.FromArgb(50, 180, 20, 30));
}
}
}
答案 0 :(得分:3)
这是您的问题:
MainWindow b = new MainWindow();
您正在创建MainWindow
的新实例,并对其进行修改,而不是用户实际可见的实例。如果您致电b.Show()
,则会看到新实例显示。
要做您想做的事,您需要引用已经存在的窗口。 documentation for Window.Show()
在这里可以提供帮助:
通过调用Show打开的窗口与打开它的窗口没有自动关系。具体来说,打开的窗口不知道哪个窗口打开了它。可以使用Owner属性建立此关系,并使用OwnedWindows属性进行管理。
从那里我们可以查看Window.Owner
的文档,其中包含一个code example。您可以在显示新窗口时执行此操作:
rooms x = new rooms();
x.Owner = this;
x.Show();
这为您提供了Owner
属性中对主窗口的引用。因此,在您的rooms
类中,您可以执行以下操作:
((MainWindow) Owner)._5.Background = new SolidColorBrush(Color.FromArgb(50, 180, 20, 30));
我假设您实际上是在某个地方声明了此_5
属性,因为您没有在代码中显示它。顺便说一句,这是一个糟糕的名字:)