我正在尝试学习 Expression Blend 和 SilverLight。我想要实现的是将所选项目从组合框中输出到文本 block 中。任何人都可以指出我正确的方向或向我展示一些 C# 是如何完成的吗?这是我当前的代码:
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();
}
最佳答案
你可以这样做:
<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}" />
只需使用一个转换器将其翻译成您的“Hello ....”字符串即可。
您也可以使用样本数据来完成。使用名为 FullName 的列创建一些示例数据。
在您的 XAML 中引用您的示例数据(与此类似)
<UserControl.Resources>
<SampleData:SampleDataSource x:Key="SampleDataSource" d:IsDataSource="True"/>
</UserControl.Resources>
那么您的 ComboBox
和 TextBlock
将更改为此。
<ComboBox x:Name="Names" DataContext="{Binding Source={StaticResource SampleDataSource}}" DisplayMemberPath="FullName" ItemsSource="{Binding Collection}"/>
<TextBlock Text="{Binding SelectedItem.FullName, ElementName=Names}" />
关于c# - 如何在 silverlight 中显示组合框的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1637240/