使用ZIndex

时间:2018-04-29 12:30:13

标签: c# wpf

我在网格上的按钮顶部有一个图像。当我点击“隐藏图像”按钮时,我想将图像放在按钮后面。我尝试过使用Panel.ZIndex,但显然它不适用于Grid。

  <Grid HorizontalAlignment="Left" Height="100" Margin="201,79,0,0" VerticalAlignment="Top" Width="100">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Button x:Name="btn_1" Grid.Column="0" Grid.Row="0" Panel.ZIndex="-1"/>
        <Image Source="Max_dog.png" Grid.Column="0" Grid.Row="0"/>

        <Button x:Name="btn_2" Grid.Column="1" Grid.Row="0"/>
        <Button x:Name="btn_3" Grid.Column="0" Grid.Row="1"/>
        <Button x:Name="btn_4" Grid.Column="1" Grid.Row="1"/>
    </Grid>
    <Button Content="Button"  Margin="215,205,228.667,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

3 个答案:

答案 0 :(得分:1)

在WPF中执行此操作的正确方法是将view model附加到窗口或控件的数据上下文中。然后将视图模型的属性绑定到图像的Visibility属性。

public class MyViewModel : INotifyPropertyChanged
{
    private bool _isImageVisible;
    public bool IsImageVisible
    {
        get { return _isImageVisible; }
        set {
            if (value != _isImageVisible) {
                _isImageVisible = value;
                OnPropertyChanged(nameof(ImageVisibility));
            }
        }
    }

    public Visibility ImageVisibility => _isImageVisible
        ? Visibility.Visible
        : Visibility.Hidden; // or Collapsed

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

在XAML中

<Image Source="Max_dog.png" Grid.Column="0" Grid.Row="0"
    Visibility="{Binding ImageVisibility}"/>

并非您无法将Visibility属性绑定到布尔值。因此,我将两个属性设置为可以轻松设置的布尔属性,另一个属性显示所需的Visibility枚举。

答案 1 :(得分:0)

我找到了解决方案

// Initializing Spark
val conf = new SparkConf().setAppName("training").setMaster("master")
new SparkContext(conf)

答案 2 :(得分:0)

WPF在堆栈布局中形成控件。与较高位置的项目相比,堆栈中较低的项目始终具有较低的zIndex。这里的解决方案是将语句声明更改为

<Grid HorizontalAlignment="Left" Height="100" Margin="201,79,0,0" VerticalAlignment="Top" Width="100">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Image Source="Max_dog.png" Grid.Column="0" Grid.Row="0"/>
        <Button x:Name="btn_1" Grid.Column="0" Grid.Row="0"/>
        <Button x:Name="btn_2" Grid.Column="1" Grid.Row="0"/>
        <Button x:Name="btn_3" Grid.Column="0" Grid.Row="1"/>
        <Button x:Name="btn_4" Grid.Column="1" Grid.Row="1"/>
    </Grid>
    <Button Content="Button"  Margin="215,205,228.667,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

看到按钮和图像的声明已被颠倒。