How to display value from a combobox in silverlight?

时光毁灭记忆、已成空白 提交于 2020-01-04 14:26:09

问题


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();
}

回答1:


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}" />



回答2:


I think you need SelectedValue instead of SelectedItem

Or

((OriginClass)Origin.SelectedItem).value;



回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/1637240/how-to-display-value-from-a-combobox-in-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!