The following code example is done by referring to the solution of vb2ae, you could refer to it to complete your project. If there is any problem, please let me know.
MainWindow.xaml:
<StackPanel>
<TextBlock x:Name="tb" Width="200" Height="50" Background="AliceBlue"/>
<ComboBox x:Name="PrinterSelection_ComboBox"
Height="80" SelectedIndex="0" Margin="5 3 5 3" Width="200"
SelectionChanged="PrinterSelection_ComboBox_SelectionChanged"
ItemsSource="{Binding PrinterList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<WrapPanel Margin="0,5,0,5" Height="45">
<Image x:Name="PrinterImage" Source="{Binding ComboBoxItemImage}"
Height="20" Stretch="Fill"
HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Label x:Name="PrinterName" Content="{Binding ComboBoxItemName}"
TextBlock.TextAlignment="Center"
VerticalAlignment="Center"/>
</WrapPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public List<MyPrinter> PrinterList { get;private set;}
public MainWindow()
{
InitializeComponent();
PrinterList = new List<MyPrinter>();
PrinterList.Add(new MyPrinter("Software602 Print2PDF", "Resources/8.jpg"));
PrinterList.Add(new MyPrinter("Microsoft XPS Document Writer", "Resources/e1.jpg"));
PrinterList.Add(new MyPrinter("Microsoft Print to PDF", "Resources/hd.jpg"));
DataContext=this;
}
string PrinterName;
private void PrinterSelection_ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = PrinterSelection_ComboBox.SelectedItem as MyPrinter;
switch (selectedItem != null)
{
case true:
PrinterName = selectedItem.ComboBoxItemName;
tb.Text=PrinterName;
break;
}
}
}
public class MyPrinter : INotifyPropertyChanged
{
private string name;
private string path;
public string ComboBoxItemName
{
get { return name; }
set
{
if (name == value) return;
name = value;
OnPropertyChanged("ComboBoxItemName");
}
}
public string ComboBoxItemImage
{
get { return path; }
set
{
if (path == value) return;
path = value;
OnPropertyChanged("ComboBoxItemImage");
}
}
public MyPrinter(string name, string path)
{
this.ComboBoxItemName = name;
this.ComboBoxItemImage = path;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
The result:
Method2:
View:
<ComboBox x:Name="PrinterSelection_ComboBox"
Height="80" SelectedIndex="0" Margin="3" Width="200"
SelectedItem="{Binding Item}"
ItemsSource="{Binding PrinterList}">
... code...
</ComboBox>
Whenever the selected item in the ComboBox is changed, so is the Item property. If you want to do something when the selected item is changed, you can do that in the property setter.
Code in MainWindow class:
string PrinterName;
private MyPrinter item;
public MyPrinter Item
{
get { return item; }
set
{
if (item != value)
{
item = value;
OnPropertyChanged("Item");
PrinterName=item.ComboBoxItemName;
tb.Text = PrinterName;
}
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.