views:

517

answers:

4

Hi I'm a designer, trying to learn blend and silverlight, basically what i'm trying to achieve is output the selected item from the comobox into a textBlock. Can any one point me in the right direction or show me some c# on how this is done. This is my current code:

private void GetSubmitBtn(object sender, System.Windows.RoutedEventArgs e)
{
    this.Message.Text = "Hello there " + this.Firstname.Text + " " + this.Surname.Text + ". You come from "  +  this.Origin.SelectedItem.ToString();     
}
A: 

I think you need SelectedValue instead of SelectedItem

Or

((OriginClass)Origin.SelectedItem).value;
Natrium
Thanks but that didn't work. When i run my code it outputs "Expression.Blend.SampleData.OriginData.Item"
ivordesign
A: 

Bind the text of your textblock to the combobox.Text.

PerlDev
that is not true, because he wants "Hello there blablabla" in his textblock, and not just the text of the combobox
Natrium
A: 

You could do something like this:

   <ComboBox x:Name="Names">
        <ComboBoxItem Content="John Doe" />
        <ComboBoxItem Content="Jane Doe" />
        <ComboBoxItem Content="Jack Black" />
        <ComboBoxItem Content="Jake White" />
        <ComboBoxItem Content="Kelly Blue" />
    </ComboBox>
    <TextBlock Text="{Binding SelectedItem.Content, ElementName=Names}" />

And just use a converter to translate into your "Hello ...." string.

You can do it with Sample Data as well. Create some sample data with a Column named FullName.

In your XAML reference your Sample Data (similar to this)

<UserControl.Resources>
 <SampleData:SampleDataSource x:Key="SampleDataSource" d:IsDataSource="True"/>
</UserControl.Resources>

Then your ComboBox and TextBlock would change to this.

<ComboBox x:Name="Names" DataContext="{Binding Source={StaticResource SampleDataSource}}" DisplayMemberPath="FullName" ItemsSource="{Binding Collection}"/>
<TextBlock Text="{Binding SelectedItem.FullName, ElementName=Names}" />
Chris Mancini
Thanks but, the combo box has sample data bound to it so this doesn't work
ivordesign
I just editing the above example to use Sample Data.
Chris Mancini
A: 

how to display text in combobox in silverlight

you can display selected value of combobox into textbox in silverlight

TextBox1.Text= (cmbApplicationStatus.SelectedItem as ComboBoxItem).Content.ToString();

here, cmbApplicationStatus is Name of your combobox

thank you

vikas sid