In the code behind I have a private List<string> _materials
I would like to display in a combox.
I need to create the data binding in Parts from the Code behind, since I'm filling _materials
through a background worker:
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 Looks like this:
<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>
"Hi" is displayed for every entry i have in my _materials
list, but the actual Name is not displayed. So What do I need to put in Text="{Binding ???}"
to get my string content displayed?