question

StefanSchmidt-6794 avatar image
0 Votes"
StefanSchmidt-6794 asked HuiLiu-MSFT commented

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

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-wpfdotnet-wpf-xaml
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@StefanSchmidt-6794. Could you show the complete code for your problem? Is it possible to describe the specific functionality you want to achieve based on the code?

0 Votes 0 ·

Hi,@StefanSchmidt-6794. Did my answer solve your problem? If so, you could accept it as an answer. It is helpful for community members with similar issues.

0 Votes 0 ·
StefanSchmidt-6794 avatar image
0 Votes"
StefanSchmidt-6794 answered

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




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered

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 to enable e-mail notifications if you want to receive the related email notification for this thread. 


7.gif (527.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.