question

Jramos avatar image
0 Votes"
Jramos asked RoyLi-MSFT commented

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

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}"  />
windows-uwp-xaml
· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you please tell me what is the excepted behavior that you want to implement? Control the text visibility of the Textbox inside the Combobox?

0 Votes 0 ·

the idea is when you choose an option from the Combobox you hide the textbox and its corresponding TextBlock or you choose another option to make that TextBlock and TexBox visible. Example as the option of the Gasoline Combobox should hide the electric car Texbox and its textBlock.

0 Votes 0 ·

Your requirement makes me confused. Where is the electric car Texbox and its textBlock? You just only have some Textlbocks in your code posted. I didn't see any textboxes.

0 Votes 0 ·
Show more comments

exactly this is the idea of hiding or making visible the corresponding TextBox and TextBlock by choosing an option from the comboBox. the problem I have been implentar gives me fault .

0 Votes 0 ·

Could you please tell me what you have tried to achieve this feature? Are you use data-binding? The easiest way is to change the Visibility of the target control in the ComboBox selected changed event.

0 Votes 0 ·

I am using the selection_Changed to hide or make visible with if and else if

0 Votes 0 ·
Show more comments

1 Answer

RoyLi-MSFT avatar image
0 Votes"
RoyLi-MSFT answered RoyLi-MSFT commented

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.








· 16
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

the code does not work for me in case that since change the SelectionChanged by SelectItem

0 Votes 0 ·

Where do you put these code? You need to put the code in the SelectionChanged event.

0 Votes 0 ·
  private void CbTipoCombustibles_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
             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;
             }
             else if(textblock.Text == "textdiesel")
             {
                 txttanqueel.Visibility = Visibility.Collapsed;
             }
             else if(textblock.Text == "textlectrico")
             {
                 txttanqueel.Visibility = Visibility.Visible;
                 txttanquec.Visibility = Visibility.Collapsed;
             }
    
    
    
    
         }

the problem is when I choose the combobox option that should hide or make visible textbox not hidden or does not make it visible

0 Votes 0 ·
Show more comments