C# 从代码隐藏进行WPF组合框数据绑定
在“代码隐藏”中,我有一个C# 从代码隐藏进行WPF组合框数据绑定,c#,wpf,xaml,data-binding,combobox,C#,Wpf,Xaml,Data Binding,Combobox,在“代码隐藏”中,我有一个私有列表_materials我想在combox中显示 我需要从代码隐藏部分创建数据绑定,因为我正在通过后台工作人员填充\u材料: public partial class MainWindow : Window { private List<string> _materials; public MainWindow() { InitializeComponent(); } private vo
私有列表_materials
我想在combox中显示
我需要从代码隐藏部分创建数据绑定,因为我正在通过后台工作人员填充\u材料
:
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;
}
公共部分类主窗口:窗口
{
私人物品清单;
公共主窗口()
{
初始化组件();
}
已加载私有无效窗口(对象发送器、路由目标)
{
BackgroundWorker工人=新的BackgroundWorker();
worker.RunWorkerCompleted+=worker\u RunWorkerCompleted;
worker.DoWork+=worker\u DoWork;
worker.RunWorkerAsync();
}
无效工作线程(对象发送器,工作线程目标)
{
_materials=DataSupplier.GetMaterials();
}
void worker\u RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
//在这里进行数据绑定吗
wpMaterial.DataContext=\u物料;
cmbMaterial.ItemsSource=\u物料;
}
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>
对于我在
\u materials
列表中的每个条目,都会显示“Hi”,但不会显示实际名称。那么我需要在Text=“{Binding???}”中输入什么才能显示我的字符串内容呢?既然您绑定的条目资源只是一个字符串集合,您只需要指定Text=“{Binding}”
由于绑定的ItemsSource只是一个字符串集合,因此只需指定Text=“{binding}”
由于绑定的ItemsSource只是一个字符串集合,因此只需指定Text=“{binding}”
由于绑定的ItemsSource只是一个字符串集合,因此只需指定Text=“{binding}”
因为\u materials
是一个字符串的列表
,这意味着每个项目都是字符串
类型,所以您要绑定到ComboBoxItem
的当前数据上下文
您可以使用{Binding}
或{Binding Path=.}
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
<TextBlock Text="Hi" />
</StackPanel>
</DataTemplate>
从
或者,可以使用句点(.)路径绑定到当前源。例如,Text=“{Binding}”相当于Text=“{Binding path=。}”
由于\u materials
是一个字符串的列表,这意味着每个项目都将是字符串类型,因此您希望绑定到ComboBoxItem
的当前数据上下文
您可以使用{Binding}
或{Binding Path=.}
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
<TextBlock Text="Hi" />
</StackPanel>
</DataTemplate>
从
或者,可以使用句点(.)路径绑定到当前源。例如,Text=“{Binding}”相当于Text=“{Binding path=。}”
由于\u materials
是一个字符串的列表,这意味着每个项目都将是字符串类型,因此您希望绑定到ComboBoxItem
的当前数据上下文
您可以使用{Binding}
或{Binding Path=.}
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
<TextBlock Text="Hi" />
</StackPanel>
</DataTemplate>
从
或者,可以使用句点(.)路径绑定到当前源。例如,Text=“{Binding}”相当于Text=“{Binding path=。}”
由于\u materials
是一个字符串的列表,这意味着每个项目都将是字符串类型,因此您希望绑定到ComboBoxItem
的当前数据上下文
您可以使用{Binding}
或{Binding Path=.}
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
<TextBlock Text="Hi" />
</StackPanel>
</DataTemplate>
从
或者,可以使用句点(.)路径绑定到当前源。例如,Text=“{Binding}”相当于Text=“{Binding path=。}”
你试过这个吗?你试过这个吗?你试过这个吗?你试过这个吗?