views:

31

answers:

3

Hi

Backgorund

I am currently writing a program that allows a user to select a manufacture from a combo box. The combo box is created in wpf using the following wpf code segment:

<ComboBox Height="23" Margin="40.422,128.423,229.908,0" Name="itemProductManufacture" ToolTip="Click to open drop down menu" VerticalAlignment="Top" Text="Select A Manufacture" SelectionChanged="itemProductManufacture_SelectionChanged" DropDownOpened="itemProductManufacture_DropDownOpened">
        <ComboBox.ItemTemplate> 
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding ManufactureId}" Width="0"/>
                    <Image Name="itemManufactureImage" Source="{Binding ManufactureImage}" Height="15" Width="70" Stretch="Uniform"/>
                    <TextBlock Text="{Binding ManufactureName}"/>
               </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

The data is provided form a database and each entry has a Image, a name and an Id (intentionally not shown)

Problem

I am trying to code the behaviour of the combo box so when it is open the image height is 50 and when it is closed it is 15 this is so the image is larger when it is first displayed and then smaller once selected so it doesn't take up too much space on the form.

I have tried editing the image propities using code but am unable to accsess it using its name or any other children of the combo box.

Thanks

Jonathan

A: 

You can edit image properties from code using binding. Or you can use triggers in Datatemplate. When comboboxitems checked properties change, you can change height property of corresponding image

Sessiz Saat
A: 

As you are using data template you won't be able to access the directly by its name.

Try something like this -

Image image = this.itemProductManufacture.ItemTemplate.FindName("itemManufactureImage", this) as Image;

One thing I am not clear is whether you want to change image size for all the items or the selected one? If you need to access the image for a particulat item in combobox you may have to use the ItemContainerGenerator.ContainerFromItem, as explained in following posts -

http://stackoverflow.com/questions/603203/wpf-itemscontrol-how-do-i-get-find-my-checkbox-item-that-is-in-the-itemtemp

http://www.sitechno.com/Blog/HowToUseAttachedPropertiesAsAnExtensionMechanismForACheckedListbox.aspx

look at this, To know the various ways of finding controls - http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls

akjoshi
A: 

Try this:

<Image Height = "{Binding Path=IsDropDownOpen, 
                          RelativeSource={RelativeSource FindAncestor, 
                                          AncestorType={x:Type ComboBox}}, 
                          Converter={StaticResource myBoolToHeightConverter}}" />

An example for Converter here

Veer