Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm trying to customize combobox, I edited its template so I can always display an image in the header of the combobox, I managed this by putting Image instead of ContentPresenter in the template, but its a template and I will create number of comboboxes from it and each have its own image, HOW I can manage that, with each new combobox i can set its own image??
(sorry for my English!):

XML
<ControlTemplate TargetType="ComboBox" x:Key="CustomBTemplate">
  <Grid>
    <Border x:Name="ContentPresenterBorder">
      <Grid>
        <ToggleButton x:Name="DropDownToggle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="-1" HorizontalContentAlignment="Right">
          <Path x:Name="BtnArrow" Height="4" Width="8" Stretch="Uniform" Margin="0,0,6,0" Fill="Black"
Data="F1 M 300,-190L 310,-190L 305,-183L 301,-190 Z " />
        </ToggleButton>
        <Image Width="35" Height="35" Source="somepicture.png" HorizontalAlignment="Left"/>
      </Grid>
    </Border>
    <Popup x:Name="Popup">
      <Border x:Name="PopupBorder" HorizontalAlignment="Stretch" Height="Auto" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="Black" Background="White" CornerRadius="3">
        <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1">
          <ItemsPresenter/>
        </ScrollViewer>
      </Border>
    </Popup>
  </Grid>
</ControlTemplate>
Posted
Updated 8-Jul-10 3:38am
v3

1 solution

You need to create an AttachedDependencyProperty that you'll apply to the top level of the ComboBox and then in your ControlTemplate you'll bind to with something like:

XML
<Image Width="35" Height="35" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(y:MyClass.ComboImage)}" HorizontalAlignment="Left"/>


You can find something very similiar dealing with brushes in my tip here.[^].
 
Share this answer
 
Comments
Andrew Rissing 28-May-10 11:55am    
By the way, since you're working in Silverlight, you might want to look at the tip regardless as it deals with some issues Silverlight has with attached dependency properties.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900