Warm tip: This article is reproduced from serverfault.com, please click

Fix an image at top right corner of wpf window irrespective of the height of grid row

发布于 2020-12-01 05:37:42

I have to place an image on top right corner of grid. But it should not take the height of grid row. Means it should appear as a top layer of grid. How can I achieve this? Doing like below, the height of grid row is increases as the height of image (Img_Data).

<Grid x:Name="LayoutRoot" Background="Black"  >
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition  Height="auto"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" ></ColumnDefinition>
        <ColumnDefinition Width="*" ></ColumnDefinition>
        <ColumnDefinition Width="*" ></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <StackPanel x:Name="menu"  Grid.Row="0" Grid.ColumnSpan="3"    >
        <Button x:Name="btnOpen" Margin="8" Padding="4" Content="Select" Click="btnOpen_Click"  Width="auto" Height="25"/>
        <Button Margin="8" Width="100" Height="28" Content="Play"  BorderBrush="Transparent" Name="btnPlay" Click="btnPlay_Click"  Style="{StaticResource CommonButton}" >
        </Button>
        <Label x:Name="lblRecordTimer" Foreground="White"  HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
        <Label x:Name="lblPlayMode" Foreground="White"   HorizontalAlignment="Center" VerticalAlignment="Center" >Play</Label>
        <Image Name="Img_Data" Source="/images/Data.png" HorizontalAlignment="Right"    Width="187" Height="150"    />
    </StackPanel>

    <StackPanel Grid.Row="1"  Grid.ColumnSpan="3"  Orientation="Vertical" HorizontalAlignment="Center">
        <Label Content="Show Details"  FontSize="18"  Foreground="White" ></Label>
        <fa:ImageAwesome Icon="Refresh" Foreground="Red"  Visibility="Collapsed" Width="50" Height="50" x:Name="spinner1" Spin="True" SpinDuration="4" />
    </StackPanel>
</Grid>
Questioner
nsds
Viewed
0
AmRo 2020-12-01 19:38:32

You can use Canvas as last child of your Grid. Like this:

<Grid x:Name="LayoutRoot" Background="Black"  >
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition  Height="auto"></RowDefinition>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" ></ColumnDefinition>
        <ColumnDefinition Width="*" ></ColumnDefinition>
        <ColumnDefinition Width="*" ></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <StackPanel x:Name="menu"  Grid.Row="0" Grid.ColumnSpan="3"    >
        <Button x:Name="btnOpen" Margin="8" Padding="4" Content="Select" Width="auto" Height="25"/>
        <Button Margin="8" Width="100" Height="28" Content="Play"  BorderBrush="Transparent" Name="btnPlay">
        </Button>
        <Label x:Name="lblRecordTimer" Foreground="White"  HorizontalAlignment="Center" VerticalAlignment="Center"></Label>
        <Label x:Name="lblPlayMode" Foreground="White"   HorizontalAlignment="Center" VerticalAlignment="Center" >Play</Label>
    </StackPanel>

    <StackPanel Grid.Row="1"  Grid.ColumnSpan="3"  Orientation="Vertical" HorizontalAlignment="Center">
        <Label Content="Show Details"  FontSize="18"  Foreground="White" ></Label>
    </StackPanel>

    <Canvas
        Grid.Row="0"
        Grid.RowSpan="2"
        Grid.Column="0"
        Grid.ColumnSpan="3">
        <Image 
            Name="Img_Data" 
            Canvas.Top="16"
            Canvas.Right="16"
            Source="/images/Data.png"
            Width="187"
            Height="150"/>
    </Canvas>
</Grid>