【问题标题】:Editing child objects of a combo box using c# and Wpf使用 c# 和 Wpf 编辑组合框的子对象
【发布时间】:2010-07-05 10:52:36
【问题描述】:

背景

我目前正在编写一个程序,允许用户从组合框中选择制造商。组合框是在 wpf 中使用以下 wpf 代码段创建的:

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

数据是从数据库中提供的,每个条目都有一个图像、一个名称和一个 ID(故意未显示)

问题

我正在尝试对组合框的行为进行编码,因此当它打开时,图像高度为 50,关闭时为 15,因此图像在第一次显示时较大,然后在选择后变小它不会在表单上占用太多空间。

我尝试使用代码编辑图像属性,但无法使用其名称或组合框的任何其他子项来访问它。

谢谢

乔纳森

【问题讨论】:

  • 您是否尝试设置 MaxHeight 属性?
  • 我做到了,但是我试图为框展开到关闭时设置不同的高度,设置组合框的最大高度属性会缩小它,而对于它覆盖的图像高度属性。

标签: c# .net wpf combobox


【解决方案1】:

当您使用数据模板时,您将无法通过其名称直接访问。

试试这样的 -

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

我不清楚的一件事是您是否要更改所有项目或选定项目的图像大小?如果您需要访问组合框中特定项目的图像,您可能必须使用 ItemContainerGenerator.ContainerFromItem,如以下帖子中所述 -

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

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

看看这个,了解查找控件的各种方法 - How can I find WPF controls by name or type?

【讨论】:

    【解决方案2】:

    您可以使用绑定从代码中编辑图像属性。或者您可以在 Datatemplate 中使用触发器。当comboboxitems选中属性发生变化时,可以改变对应图片的height属性

    【讨论】:

      【解决方案3】:

      试试这个:

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

      An example for Converter here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-16
        • 2016-04-18
        • 1970-01-01
        相关资源
        最近更新 更多