A.Pissicat A.Pissicat - 2 months ago 17
C# Question

Fix a ComboBox to an Image

I'm using an

Image
with a
ComboBox
in my C# WPF application.

I want the
ComboBox
fixed in top/right corner of
Image
(not the grid containing both). Actually these two elements are in a grid.

It's hard for me to clearly explain what I want, there is pictures to help me.

What I want:

resizeOK

What I have:

resizeNOK

How can I write my
ComboxBox
to achive this ?

<Grid>
<Image HorizontalAlignment="Stretch" x:Name="VideoControl" FlowDirection="LeftToRight"/>
<ComboBox Grid.Row="1" x:Name="ListCameraDevices" Style="{StaticResource {x:Static ToolBar.ComboBoxStyleKey}}"
HorizontalAlignment="Right" VerticalAlignment="Top"
Width="auto"
Background="Transparent" BorderBrush="Transparent" Foreground="White"
BorderThickness="0"/>
</Grid>

Answer

Gaaty's answer is mostly right but the column definitions are not needed. You simply need to ensure the Image has it's Stretch property set to 'Uniform' so that it sizes the inner grid correctly.

Here's a simplified version:

<Grid>
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Image Source="https://a2ua.com/awesome/awesome-004.jpg" Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
        <ComboBox HorizontalAlignment="Right" VerticalAlignment="Top" Background="Transparent" BorderBrush="Transparent" Foreground="White" BorderThickness="0"/>
    </Grid>
</Grid>
Comments