Search code examples
c#wpfcomboboxz-index

zIndex on ComboBox in WPF?


I need to have three combo boxes in a same data grid cell, like so:

<Grid Name="gridFormat_1" Grid.Row="1" Grid.Column="6" Margin="0, 5, 5, 5" >
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <ComboBox Grid.Row="0" Name="monthsCB" DisplayMemberPath="MonthName" IsEnabled="False" Opacity="0" />
    <ComboBox Grid.Row="0" Name="quartalsCB" DisplayMemberPath="QuartalNumber" IsEnabled="False" Opacity="0" />
    <ComboBox Grid.Row="0" Name="yearsCB" DisplayMemberPath="Year" IsEnabled="False" Opacity="0" />
</Grid>

I want to be able to show/hide certain combo box at a given time. However, although Opacity and IsEnabled, do hide/show, disable/enable each combo box except the one I chose, I am not able to click on the combo boxes below the "yearsCB" since it is added last.

This means other two combo boxes do not receive mouse events.

I found that I should use some other WPF tag to wrap my combo boxes so I can switch the ZIndex on each of them.

Which tag would be that, which will allow me to manipulate Canvas or ZIndex?

Or is there some other better way to achieve this?


Solution

  • If you need to set ZIndex, you can do it with attribute

    Panel.ZIndex="1"
    

    Higher the number, higher the item. Default ZIndex is 0.

    In WPF you position elements inside their parent - in this case parent is Grid. Grid class is derived from Panel class and Panel has ZIndex attached property.

    If you want to switch between 3 comboboxes in one position you should change their Visiblity. Opacity only changes visual state and IsEnabled sets if element is enabled in UI.

    For hidden combobox:

    Visibility="Hidden"
    

    For visible combobox:

    Visibility="Visible"