简体   繁体   中英

Editing child objects of a combo box using c# and Wpf

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

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

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 -

WPF - ItemsControl - How do I get find my "CheckBox" item that is in the ItemTemplate?

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

look at this, To know the various ways of finding controls - How can I find WPF controls by name or type?

Try this:

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

An example for Converter here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM