c# - 使用 C# 和 Wpf 编辑组合框的子对象

标签 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="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,因此图像在首次显示时较大,然后在选择后变小,因此它不会在表单上占用太多空间。

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

谢谢

乔纳森

最佳答案

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

尝试这样的事情 -

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?

关于c# - 使用 C# 和 Wpf 编辑组合框的子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3178896/

相关文章:

c# - 如何模拟低电平鼠标左键点击,让微软虚拟键盘接受?

c# - WPF - 如何通过绑定(bind)停止计时器

WPF TreeView : Using ItemTemplateSelector and ItemContainerStyle Not Working At Same Time

WPF/MVVM : Delegating a domain Model collection to a ViewModel

c# - 如何转换 JToken

c# - 为什么会出现 Service Bus 链接日志?

c# - 无法创建接口(interface)的实例 (PagedList)

c# - ConcurrentDictionary Keys 或 Values 属性是线程安全的

c# - 如果目录不存在如何创建文件?

c# - 使用 SolidWorks API 打开 .dwg