创建多个面板并在 wpf 中单击按钮时显示一个面板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11827575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:51:58  来源:igfitidea点击:

creating multiple panels and displaying one panel on button click in wpf

c#wpfvisual-studio-2010

提问by Shee

I am new to WPF and I want to create a WPF application with 5buttons. On the click of each button I want a content to be displayed on another panel. Right now I just want different images to be displayed on my right side panel on the button clicks.

我是 WPF 的新手,我想创建一个带有 5 个按钮的 WPF 应用程序。单击每个按钮时,我希望在另一个面板上显示内容。现在我只想在单击按钮时在右侧面板上显示不同的图像。

Here's my XAML code:

这是我的 XAML 代码:

<Window x:Class="GridButton.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MyFirstApp" Height="350" Width="525" Loaded="Window_Loaded">
<Viewbox Stretch="Fill" StretchDirection="Both">
<DockPanel>
    <StackPanel DockPanel.Dock="left" Margin="5" Width="Auto" VerticalAlignment="Center" Height="Auto">
        <Button  Content="1" Name="button2" Click="button2_Click">
       </Button>
        <Button Content="2" Name="button1" Click="button1_Click_1">
</Button>
        <Button Content="3" Name="button3"  Click="button3_Click">
          </Button>
        <Button Content="4" Name="button4" Margin="5">
          </Button>
        <Button Content="5" Name="button5" Margin="5" Click="button5_Click_1">
          </Button>
    </StackPanel>
        <StackPanel DockPanel.Dock="Right">
            <Image Name="img1" Source="Blue Hills.jpg" Stretch="Uniform" Visibility="Hidden" ImageFailed="Image_ImageFailed" Height="257" />

        </StackPanel>

</DockPanel>

And my xaml.cs file contains code to display image:

我的 xaml.cs 文件包含显示图像的代码:

private void button2_Click(object sender, RoutedEventArgs e)
{

    img1.Visibility = Visibility.Visible;
}

I could get only this far.

我只能走到这一步。

回答by Clemens

You can set the Sourceproperty of the Imagecontrol in code:

您可以在代码中设置控件的Source属性Image

private void buttonx_Click(object sender, RoutedEventArgs e) 
{
    string path = ... // path to image file here
    img1.Source = new BitmapImage(new Uri(path));
}

You could easily reuse the same Click handler for all Buttons and check which one was pressed:

您可以轻松地为所有按钮重用相同的 Click 处理程序并检查按下了哪个按钮:

private void Button_Click(object sender, RoutedEventArgs e) 
{
    Button button = sender as Button;
    string path = null;
    if (button == button1)
    {
        path = ... // path to image file 1 here
    }
    else if ...

    if (path != null)
    {
        img1.Source = new BitmapImage(new Uri(path));
    }
}

If you want to remove the a child Panel (or other control) from a parent Panel and add another one, you would have to modify the Panel's Childrenproperty:

如果要从父面板中删除子面板(或其他控件)并添加另一个面板,则必须修改面板的Children属性:

<StackPanel Name="parent">   
    <StackPanel Name="child" />   
</StackPanel>  

parent.Children.Remove(child);
parent.Children.Add(...); // some other control here

This approach would usually make sense if you wanted to create child panels dynamically. If you want to declare everything in XAML you may put all child panels in a Grid and change their visibility as you did already.

如果您想动态创建子面板,这种方法通常很有意义。如果您想在 XAML 中声明所有内容,您可以将所有子面板放在 Grid 中,并像之前一样更改它们的可见性。

However, you might also change the ZIndexattached property.

但是,您也可以更改ZIndex附加属性。

<Grid>
    <StackPanel Name="child1">
    </StackPanel>
    <StackPanel Name="child2">
    </StackPanel>
    <StackPanel Name="child3">
    </StackPanel>
</Grid>

child3 is topmost by default, but now you can set ZIndexto some value > 0 to make another child topmost:

child3 默认是最上面的,但是现在你可以设置ZIndex为某个值 > 0 来让另一个孩子最上面:

private void Button_Click(object sender, RoutedEventArgs e) 
{
    ...
    // reset ZIndex on previous topmost panel to 0 before
    Panel.SetZIndex(child1, 1);
}

Or completely omit the Button/Grid/Panel design and use a TabControl.

或者完全省略 Button/Grid/Panel 设计并使用TabControl