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

Stefan Schmidt 1 Reputation point
2022-04-14T23:26:17.697+00:00

Hello

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
XAML
XAML
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
    2022-04-15T13:12:37.227+00:00

    Hey

    i try to describe the matter as following

    <ControlTemplate x;TargetType="ComboBox">
    <Grid>
    <Image>
    <Image.Source>
    ///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)"/>
    </ImageSource>
    </Image>
    </>Grid>
    </ControlTemplate>

    <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:

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

    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
    2022-04-18T06:46:37.717+00:00

    You could try to refer to the code below.
    MainWindow.xaml:

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

    MainWindow.xaml.cs:

    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Runtime.CompilerServices;  
    using System.Windows;  
    
    namespace ComboboxTemplateDemo  
    {  
      public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
      }  
      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; }  
          set  
          {  
            selectedLangComboItem=value;  
            OnPropertyChanged("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:
    193710-7.gif


    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. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments