【发布时间】:2014-12-09 10:44:14
【问题描述】:
在后面的代码中,我有一个 private List<string> _materials 我想在组合框中显示。
我需要在 Parts from the Code behind 中创建数据绑定,因为我正在通过后台工作人员填写 _materials:
public partial class MainWindow : Window
{
private List<string> _materials;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
_materials = DataSupplier.GetMaterials();
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Do Databinding Here
wpMaterial.DataContext = _materials;
cmbMaterial.ItemsSource = _materials;
}
XAML 看起来像这样:
<WrapPanel x:Name="wpMaterial" >
<Label Content="Material: " FontStyle="Italic" FontFamily="Arial" Foreground="Black" Background="{x:Null}" Width="100" />
<ComboBox Name="cmbMaterial">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="Hi" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</WrapPanel>
_materials 列表中的每个条目都会显示“Hi”,但不会显示实际名称。那么我需要输入什么Text="{Binding ???}" 才能显示我的字符串内容?
【问题讨论】:
标签: c# wpf xaml data-binding combobox