WPF C# 在运行时向 ComboBox 添加文本和图像
WPF C# add text and image to ComboBox during runtime
基本上,当用户单击一个按钮时,我想在 ComboBox
中添加当前 运行 应用程序的名称列表,并在它们旁边添加图标。我知道如何获取我的应用程序列表和我的图标,但不确定如何 link 将所有内容放在一起。 ComboBox
的 XAML 当前看起来如下所示:
<ComboBox x:Name="dial1AppSelection" Grid.Column="3" Grid.Row="4" MinHeight="25" Height ="25" MaxHeight="35" MinWidth="120" Margin="4,0,0,0" SelectionChanged="dial1AppSelection_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Height="20" Source="???" />
<TextBlock ><Run Text="???" /></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
- 添加到您的组合框
ItemsSource="{Binding YourCollection}"
- 为您的对象创建 class:
public class MyProcess
{
public ImageSource Image { get; set; }
public string Text { get; set; }
}
- 将此类代码添加到您的 MainWindow.xaml.cs
public ObservableCollection<MyProcess> YourCollection { get; set; }
public MainWindow()
{
InitializeComponent();
YourCollection = new ObservableCollection<MyProcess>();
YourCollection.Add(new MyProcess() { Image ="image1", Text = "txt1"});
DataContext = this;
}
- 将字段名称插入您的 xaml 代码:
Source="{Binding Path=Image}"
Text="{Binding Path=Text}"
基本上,当用户单击一个按钮时,我想在 ComboBox
中添加当前 运行 应用程序的名称列表,并在它们旁边添加图标。我知道如何获取我的应用程序列表和我的图标,但不确定如何 link 将所有内容放在一起。 ComboBox
的 XAML 当前看起来如下所示:
<ComboBox x:Name="dial1AppSelection" Grid.Column="3" Grid.Row="4" MinHeight="25" Height ="25" MaxHeight="35" MinWidth="120" Margin="4,0,0,0" SelectionChanged="dial1AppSelection_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Height="20" Source="???" />
<TextBlock ><Run Text="???" /></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
- 添加到您的组合框
ItemsSource="{Binding YourCollection}"
- 为您的对象创建 class:
public class MyProcess
{
public ImageSource Image { get; set; }
public string Text { get; set; }
}
- 将此类代码添加到您的 MainWindow.xaml.cs
public ObservableCollection<MyProcess> YourCollection { get; set; }
public MainWindow()
{
InitializeComponent();
YourCollection = new ObservableCollection<MyProcess>();
YourCollection.Add(new MyProcess() { Image ="image1", Text = "txt1"});
DataContext = this;
}
- 将字段名称插入您的 xaml 代码:
Source="{Binding Path=Image}"
Text="{Binding Path=Text}"