How to reference inside a controltemplate of a combobox or listbox to the current selecteditem

Stefan Schmidt 1 Reputation point


i have at sec no idea,, i am inside the common template of a Combobox, so i wanna on the left side of the tooglebutton to place anb image to display the content of the current selected ComboBoxitem as image inside the combobox area. I have somehow no idea how to exactly to bind a property of that image ,. laying inside the combobox template ,, also which syntax is used. As relativesource, or ancestor type to refer to the selecteditem and then a property of the same.

THX already for reading and may answering

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,694 questions
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
778 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Stefan Schmidt 1 Reputation point


    i try to describe the matter as following

    <ControlTemplate x;TargetType="ComboBox">
    ///Below this ControlTemplate nests a Template of type ComboBoxItem , if this ComboBox changes the selctedItem property, i
    /// want to change the sopurce of this Image with the source property of the Image inside the ComboBocItem Template,,, for that i
    /// have there applied an attachedproperty to can access the property
    <Binding Source="{Binding to current selected items attached property (Excatlöy the syntax for such is me missing)"/>

    <Style TargetType="ComboBoxItem">
    <Setter Property="local:Namespace.AttachedProperty" Value="{x:Null"/>
    <Setzter Property=ControlTemplate> <Setter.Value> <ControlTemplate TargetType="ComboBoxItem"> <Grid> //here the AttachedProperty is set in the ComboBox Template <Image Source="local:Namespace.AttachedProperty"/> <Label Content={TemplateBinding Content}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter></Style>

    And then in the xaml design sheet:

    <ComboBoxItem local:Namespace.AttachedProperty="URL"/>
    <ComboBoxItem local:Namespace.AttachedProperty="URL"/>
    <ComboBoxItem local:Namespace.AttachedProperty="URL"/>

    Me in all is asking how to refer to the current selected item in the first ControlTemplate, if the user changes the item selected in the ComboBox the Source of the image also should changes,, i mean there directly the binding syntax to refer to the current selected item

    0 comments No comments

  2. Hui Liu-MSFT 46,961 Reputation points Microsoft Vendor

    You could try to refer to the code below.

            <local:ViewModel />  
            <ComboBox SelectedItem="{Binding SelectedLangComboItem}" ItemsSource="{Binding Languages}">  
                        <StackPanel Orientation="Horizontal">  
                            <Image Source="{Binding Image}" />  
                            <TextBlock Text="{Binding Label}" />  


    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Runtime.CompilerServices;  
    using System.Windows;  
    namespace ComboboxTemplateDemo  
      public partial class MainWindow : Window  
        public MainWindow()  
      public class ViewModel : INotifyPropertyChanged  
        public ObservableCollection<LanguageItem> Languages{ get;set;}  
        public ViewModel()  
          Languages = new ObservableCollection<LanguageItem>();  
          Languages.Add(new LanguageItem("spring.jpg", "content1"));  
          Languages.Add(new LanguageItem("spring1.jpg", "content2"));  
          Languages.Add(new LanguageItem("images.jpg", "content3"));  
        private LanguageItem selectedLangComboItem;  
        public LanguageItem SelectedLangComboItem  
          get { return selectedLangComboItem; }  
        private void OnPropertyChanged([CallerMemberName] string name = null)  
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));  
        public event PropertyChangedEventHandler PropertyChanged;  
      public class LanguageItem  
        public string Image { get; set; }  
        public string Label { get; set; }  
        public LanguageItem(string image, string label)  
          Image = image;  
          Label = label;  

    The result:

    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 


    0 comments No comments