-1

I am new to WPF. I want to know how can i get the selectedtext if the text that i want is placed inside a control inside comboboxitem and not directly inside comboboxitem. I know this is very basic,but would appreciate if someone could provide me a simple and quick solution

My simple combobox code is as follows:

<ComboBox x:Name="cmdTest">
    <ComboBoxItem>
        <TextBlock Text="RED" Background="RED" Foreground="White" FontFamily="Sans Serif" FontSize="14"/>
    </ComboBoxItem>
    <ComboBoxItem>
        <Grid Width="250">
            <TextBlock Text="GREEN" Background="GREEN" Foreground="White" FontFamily="Sans Serif" FontSize="14" Height="15"/>
        </Grid>
    </ComboBoxItem>
    <ComboBoxItem>
        <TextBlock Text="BLUE" Background="Blue" Foreground="White" FontFamily="Sans Serif" FontSize="14"/>
    </ComboBoxItem>
</ComboBox>
mm8
  • 163,881
  • 10
  • 57
  • 88
Jatinder Walia
  • 143
  • 1
  • 12

3 Answers3

0

XAML:

<ComboBox Name="anyname" SelectionChanged="cmbColors_SelectionChanged">

C#:

private void cmbColors_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    MessageBox.Show(string.Format("Selected Item: {0}", anyname.Text));
}
mm8
  • 163,881
  • 10
  • 57
  • 88
0

Try this:

private void cmdTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBoxItem item = cmdTest.SelectedItem as ComboBoxItem;
    if (item != null)
    {
        TextBlock txt = item.Content as TextBlock;
        if (txt != null)
            MessageBox.Show(txt.Text + " is selected");
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • tx..this worked for me for now. :-). just one more question .. when bi run my application and the combobox shows up with the current xaml,though i have applied the HorizontalContentAlignment="Stretch" attribute in the combobox as suggested above,the background color of the comboboxitem still leaves a few pixels uncolored on either side of the combobox item. Is there some xaml attribute or anything that can cover that too? tx in advance`:-). – Jatinder Walia Apr 01 '17 at 05:57
  • Please ask a new question if you have another issue. – mm8 Apr 02 '17 at 17:03
0

First change your xaml code:<ComboBox x:Name="cmdTest" HorizontalContentAlignment="Stretch" SelectedValuePath="Text"> <ComboBoxItem Padding="0" > <TextBlock Text="RED" Background="RED" Foreground="White" FontFamily="Sans Serif" FontSize="14"/> </ComboBoxItem> <ComboBoxItem Padding="0"> <TextBlock Text="GREEN" Background="GREEN" Foreground="White" FontFamily="Sans Serif" FontSize="14" Height="15"/> </ComboBoxItem> <ComboBoxItem Padding="0"> <TextBlock Text="BLUE" Background="Blue" Foreground="White" FontFamily="Sans Serif" FontSize="14"/> </ComboBoxItem> </ComboBox>

After for get text value use:

 var z = cmdTest.Text.ToString();
            MessageBox.Show(z);
  • tx..this worked for me for now. :-). just one more question .. when i run my application and the combobox shows up with the current xaml,though i have applied the HorizontalContentAlignment="Stretch" attribute in the combobox as suggested above,the background color of the comboboxitem still leaves a few pixels uncolored on either side of the combobox item. Is there some xaml attribute or anything that can cover that too? tx in advance`:-). – Jatinder Walia Apr 01 '17 at 06:38