I'm trying to learn Expression Blend and SilverLight. What I'm trying to achieve is output the selected item from the combobox 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();
}
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}" />
I think you need SelectedValue
instead of SelectedItem
Or
((OriginClass)Origin.SelectedItem).value;
Bind the text of your textblock to the combobox.Text.
you can display selected value of combobox into textbox in silverlight as follows:
TextBox1.Text = (cmbApplicationStatus.SelectedItem as ComboBoxItem).Content.ToString();
here, cmbApplicationStatus
is the name of your combobox
精彩评论