我有一个主窗口,其中包含一个包含一些按钮的堆栈面板。对于每个按钮,我有一个相关的网格面板,而该网格面板又有几个图像按钮。单击网格面板图像按钮将打开一个新窗口。在主窗口中,我的网格可见性设置为隐藏(因为我希望仅在从堆栈面板中单击其中一个按钮时才显示网格)但是当我单击新窗口上的后退按钮时,我想要选择的网格可见。如何实现这一目标?我没有使用MVVM。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Custom="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication5.Window1"
Title="Home_window" Height="456.163" Width="653.173" Background="#FF9CB5D3">
<Window.Resources>
<ResourceDictionary>
<BitmapImage x:Key="buttonicon" UriSource="/Images/icon.png"/>
</ResourceDictionary>
</Window.Resources>
<Grid x:Name="menu" HorizontalAlignment="Left" Height="131" Margin="1,156,0,-211" VerticalAlignment="Top" Width="123">
<StackPanel x:Name="menu" HorizontalAlignment="Left" Height="150" VerticalAlignment="Top" Width="123" Margin="13,30,-13,-49">
<Button Content="Home" Height="25" Click="Button_Click_1" >
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF3CABEA"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.322"/>
<GradientStop Color="#FF62A7CF" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button Content="b" Height="25" Click="Button_Click_8">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF3CABEA" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.322"/>
<GradientStop Color="#FF62A7CF" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button Content="c" Height="25" Click="Button_Click_9">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF3CABEA" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.322"/>
<GradientStop Color="#FF62A7CF" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button Content="d" Height="25" Click="Button_Click_10">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF3CABEA" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.322"/>
<GradientStop Color="#FF62A7CF" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button Content="e" Height="25" Click="Button_Click_11">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF3CABEA" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.322"/>
<GradientStop Color="#FF62A7CF" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
</StackPanel>
</Grid>
<Grid x:Name="grid_home" HorizontalAlignment="Left" Height="260" Margin="167,156,-123,-105" VerticalAlignment="Top" Width="445" Background="#FF577EAC" Visibility="Hidden">
<Button Margin="35,29,305,160" Click="Button_Click_2">
<StackPanel x:Name="inventory" Orientation="Horizontal" Height="66">
<Image Source="images/inventory.png" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,12,0,10"/>
<TextBlock Text=" Inventory" FontSize="12" Width="67" Height="19" Margin="0,24,0,23"/>
</StackPanel>
</Button>
<Button Margin="167,29,173,160" Click="Button_Click_3" >
<StackPanel x:Name="supplier" Orientation="Horizontal" Height="66">
<Image Source="images/supplier.jpg" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,12,0,10"/>
<TextBlock Text=" Supplier" FontSize="12" Width="67" Height="19" Margin="0,24,0,23"/>
</StackPanel>
</Button>
<Button Margin="302,29,38,160" Click="Button_Click_4">
<StackPanel x:Name="purchase order" Orientation="Horizontal" Height="66">
<Image Source="images/po.jpg" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,9,0,13"/>
<TextBlock FontSize="12" Width="68" Margin="0,17,0,15"><Run Text=" Purchase"/><LineBreak/><Run Text=" Order"/></TextBlock>
</StackPanel>
</Button>
<Button Margin="35,141,305,49" Click="Button_Click_5">
<StackPanel x:Name="shipping" Orientation="Horizontal" Height="66" Width="101">
<Image Source="images/shipping.png" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,6,0,16"/>
<TextBlock FontSize="12" Width="68" Margin="0,17"><Run Text=" Shipping "/></TextBlock>
</StackPanel>
</Button>
<Button Margin="166,142,174,48" Click="Button_Click_6" >
<StackPanel x:Name="sales" Orientation="Horizontal" Height="66">
<Image Source="images/sales.jpg
" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,7,0,15"/>
<TextBlock FontSize="12" Width="68" Margin="0,20,0,14" Text=" Sales "/>
</StackPanel>
</Button>
<Button Margin="303,143,38,47" Click="Button_Click_7" >
<StackPanel x:Name="purchase report" Orientation="Horizontal" Height="66">
<Image Source="images/pr.jpg" Stretch ="Uniform" Width="35" RenderTransformOrigin="1.343,-0.22" Margin="0,8,0,14"/>
<TextBlock FontSize="12" Width="68" Margin="0,14,0,17"><Run Text=" Purchase "/><LineBreak/><Run Text=" Reports"/></TextBlock>
</StackPanel>
</Button>
</Grid>
<Grid x:Name="grid_b" HorizontalAlignment="Left" Height="260" Margin="167,156,-123,-105" VerticalAlignment="Top" Width="445" Background="#FFB5CDEA" Visibility="Hidden">
</Grid>
在选择Home选项时,它会显示grid_home。在grid_home上点击库存按钮,它会打开库存窗口:
<Window x:Class="WpfApplication5.inventory"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Inventory" Height="300" Width="469.611">
<Grid Margin="0,0,2,0">
<Button Content="Back" HorizontalAlignment="Left" Margin="195,223,0,0" VerticalAlignment="Top" Width="75" Height="37" Click="Button_Click_1"/>
主窗口代码隐藏的一部分:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
grid_home.Visibility = Visibility.Visible;
grid_b.Visibility = Visibility.Collapsed;
}
private void Button_Click_8(object sender, RoutedEventArgs e)
{
inventory obj = new inventory();
obj.Show();
this.Close();
}
库存窗口背后的部分代码:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Home_window obj = new Home_window();
obj.Show();
this.Close();
grid_home.Visibility = Visibility.Visible;
}
}
}