C# 使用c和Wpf编辑组合框的子对象
背根 我目前正在编写一个程序,允许用户从组合框中选择制造商。组合框是使用以下wpf代码段在wpf中创建的:C# 使用c和Wpf编辑组合框的子对象,c#,.net,wpf,combobox,C#,.net,Wpf,Combobox,背根 我目前正在编写一个程序,允许用户从组合框中选择制造商。组合框是使用以下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
<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。这样一来,图像在第一次显示时会变大,选中后会变小,这样就不会在表单上占用太多空间
我已经尝试过用代码编辑图片,但无法使用其名称或组合框的任何其他子项访问它
谢谢
Jonathan您可以使用绑定从代码中编辑图像属性。也可以在Datatemplate中使用触发器。当comboboxitems选中属性更改时,您可以更改相应图像的高度属性您可以使用绑定从代码编辑图像属性。也可以在Datatemplate中使用触发器。当comboboxitems checked属性更改时,您可以更改相应图像的height属性,因为您使用的是数据模板,您将无法通过其名称直接访问 试试这样的东西-
Image image = this.itemProductManufacture.ItemTemplate.FindName("itemManufactureImage", this) as Image;
我不清楚的一点是,您是要更改所有项目的图像大小,还是要更改所选项目的图像大小?如果您需要访问combobox中某个特定项目的图像,您可能必须使用ItemContainerGenerator.ContainerFromItem,如下文所述-
看看这个,要了解查找控件的各种方法-当您使用数据模板时,您将无法直接通过其名称访问 试试这样的东西-
Image image = this.itemProductManufacture.ItemTemplate.FindName("itemManufactureImage", this) as Image;
我不清楚的一点是,您是要更改所有项目的图像大小,还是要更改所选项目的图像大小?如果您需要访问combobox中某个特定项目的图像,您可能必须使用ItemContainerGenerator.ContainerFromItem,如下文所述-
看看这个,要了解查找控件的各种方法-试试以下方法:
<Image Height = "{Binding Path=IsDropDownOpen,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ComboBox}},
Converter={StaticResource myBoolToHeightConverter}}" />
试试这个:
<Image Height = "{Binding Path=IsDropDownOpen,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ComboBox}},
Converter={StaticResource myBoolToHeightConverter}}" />
您是否尝试设置MaxHeight属性?我尝试过,但我正在尝试设置不同的高度,用于将框扩展到关闭时,为组合框设置max height属性会使其收缩,并为其覆盖height属性的图像设置max height属性。您尝试过设置MaxHeight属性吗?我尝试过,但是我试图为框扩展到闭合时设置不同的高度,为组合框设置“最大高度”属性会使其收缩,并为其覆盖“高度”属性的图像设置不同的高度。