hide or make visible textblock and textbox by using different options of the Combobox

Javier R 211 Reputation points
2021-08-22T16:15:05.18+00:00

Both the Textblock and Texbox must be hidden or visible when you snoth one of the combobox's dire options. both the textblock and texbox must be hidden or visible when you snoth one of the combobox's dire options. example of textblock and texbox to hide and make visible txttanqueel(textbox) and textbLock(txtelectric)

ComboBox x:Name="CbTipocombustibles"  SelectionChanged="CbTipoCombustibles_SelectionChanged"   Width="200"  Margin="0,12,0,0" >
                        <ComboBoxItem IsSelected="true">
                            <TextBlock x:Name="textgasolina" x:Uid="gasolina" FontSize="{StaticResource SmallFontSize}"/>
                        </ComboBoxItem>
                        <ComboBoxItem >
                            <TextBlock x:Name="textdiesel" x:Uid="diesel" FontSize="{StaticResource SmallFontSize}"/>
                        </ComboBoxItem>
                        <ComboBoxItem>
                            <TextBlock x:Name="textlectrico" x:Uid="Electrico" FontSize="{StaticResource SmallFontSize}"/>
                        </ComboBoxItem>
                        <ComboBoxItem>
                            <TextBlock x:Name="textgasoelectrico" x:Uid="gasolina_electrico" FontSize="{StaticResource SmallFontSize}"/>
                        </ComboBoxItem>
                        <ComboBoxItem>
                            <TextBlock x:Name="textdielectrico" x:Uid="diesel_electrico" FontSize="{StaticResource SmallFontSize}"/>
                        </ComboBoxItem>
                    </ComboBox>

xaml

TextBlock x:Name="txtcombustible"   x:Uid="CapacidadCombustible" Margin="0,21,0,0"  Grid.Row="26" Style="{StaticResource TextTextStyle}"/>
                    <TextBox x:Name="txttanquec"  Text=""    Width="120" Margin="-200,23,0,0" FontSize="{StaticResource SmallFontSize}"/>

                    <TextBlock x:Name="txtelectrico"  x:Uid="Capacidadelectrico" Margin="0,25,0,0" Style="{StaticResource TextTextStyle}"  />
                    <TextBox x:Name="txttanqueel"   Text=""    Width="120" Margin="-200,27,0,0" FontSize="{StaticResource SmallFontSize}"  />
Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 31,826 Reputation points Microsoft Vendor
    2021-08-30T07:39:48.84+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Based on the code you provided, here is some basic information you need to know. The CbTipocombustibles.Name is just the name of the ComboBox, it is not the value that users selected. You need to get the value from the TextBlock inside the ComboBoxItem. Then you could check that value to decide if you need to hide the TextBox or TextBlock.

    Please try the following code:

     ComboBox CbTipocombustibles = (ComboBox)sender;  
                //get selected comboboxitem  
                ComboBoxItem item = (ComboBoxItem)CbTipocombustibles.SelectedItem;  
                //get textblock inside the comboboxitem  
                TextBlock textblock = (TextBlock)item.Content;  
                //get the value which is selected  
                if (textblock.Text == "textgasolina")  
                {  
                    txttanqueel.Visibility = Visibility.Collapsed;  
                }  
    

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.