我有一个项目,我希望MainWindow中的按钮在加载另一个窗口时是可见的,如果此窗口关闭则折叠
我试过这种方式
Xaml for MainWindow
<Grid >
<Button x:Name="button" Content="Opne Window1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="button_Click" Margin="220,110,0,0"/>
<Button x:Name="WinBut1" Content="Check order" HorizontalAlignment="Left" VerticalAlignment="Top" Visibility="Hidden" Width="75" Click="button9_Click" Margin="10,1,0,0"/>
</Grid>
MainWindow控件的代码隐藏中的:
public partial class MainWindow : Window
{
Window1 Window1 = new Window1();
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
//this.Window1.Loaded += new RoutedEventHandler (CheckWindow);
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Window1 childWindow = new Window1();
childWindow.MyEvent += new EventHandler(childWindow_MyEvent);
childWindow.ShowDialog();
}
void childWindow_MyEvent(object sender, EventArgs e)
{
if (Window1 != null)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(
delegate ()
{
WinBut1.Visibility = Visibility.Visible;
}
));
}
}
//void CheckWindow(object sender, RoutedEventArgs e)
//{
// if (Window1 != null && Window1.Visibility == Visibility.Visible)
// {
// Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(
// delegate ()
// {
// WinBut1.Visibility = Visibility.Visible;
// }
// ));
// }
//}
private void button_Click(object sender, RoutedEventArgs e)
{
new Window1().Show();
}
}
Window1控件的代码隐藏中的:
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(win1_Loaded);
}
public event EventHandler MyEvent;
protected void OnMyEvent()
{
if (this.MyEvent != null)
this.MyEvent(this, EventArgs.Empty);
}
void win1_Loaded(object sender, RoutedEventArgs e)
{
this.OnMyEvent();
}
}
答案 0 :(得分:0)
非常简单,您只需要IsVisibleChanged
Window1
中的MainWindow
,并将按钮的可见性设置为窗口1的可见性。
您的MainWindow.cs:
public partial class MainWindow : Window
{
Window1 window1;
public MainWindow()
{
InitializeComponent();
window1 = new Window1();
window1.IsVisibleChanged += Window1_IsVisibleChanged;
this.Closing += MainWindow_Closing;
}
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Application.Current.Shutdown();//will close all windows
}
private void Window1_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
buttonCollapsed.Visibility = window1.Visibility;
}
private void buttonShow_Click(object sender, RoutedEventArgs e)
{
window1.Show();
}
和Window1.cs:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.Closing += Window1_Closing;
}
private void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(sender is Window1)
{
e.Cancel = true;//Cancel the closing
this.Hide();
}
}
private void buttonHide_Click(object sender, RoutedEventArgs e)
{
this.Hide();//hide your window --> IsVisibilityChanged-Event will be raised
}
}
答案 1 :(得分:0)
嗯,Window类对象中有两个事件Loaded
和Closed
。
您需要为它们订阅两个事件处理程序。一切都在你的Window类中完成。
根据您的要求,您可以执行以下操作:
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 win = new Window1();
win.Loaded += win_Loaded;
win.Closed += win_Closed;
win.ShowDialog();
}
void win_Closed(object sender, EventArgs e)
{
this.but.Visibility = Visibility.Visible;
}
void win_Loaded(object sender, RoutedEventArgs e)
{
this.but.Visibility = Visibility.Hidden;
}
这个代码用在后面的窗口代码中。