WPF组合框,带有图片,文字和标识 [英] WPF combobox with pictures, text and ident

查看:93
本文介绍了WPF组合框,带有图片,文字和标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我们目前有一个源自Windws.Form.Combobox的Windows.Forms.Combobox。


此控件显示对于combox中的每个条目


< dent(if> 0)>< Picture(如果存在)>< Text(always)>


我们覆盖此



 protected override void OnDrawItem(DrawItemEventArgs e)



如何实现相同的WPF?


tia


  Hendrik Schmieder



解决方案

组合框是一个itemscontrol。


这意味着你可以将itemssource绑定到任何对象的集合。


该对象可以有属性。


您可以将每个模板模板化为组合中的项目。



我将定义一个表示数据的类进入每个项目。


调用ItemVM。


这将为你想绑定的每个属性提供公共属性。


一种是某种图片。


您可以为图片或实际图片公开网址,并将其绑定到图像控件的源。


然后你有一些文字。


这是一个字符串属性。


如果你想把它们排成一行您可以使用具有两个列定义的网格。


或者它们可能不应该排列,在这种情况下,您可以使用方向水平的堆栈面板。


您似乎也不想根据我不完全遵循的某些标准来显示图片。


您可以使用数据触发器来驱动它。


将Visibility属性设置为Collapsed或Hidden。


您可以从此公开可见性枚举(它不仅仅是wpf中的bool)或使用转换器从bool转换为false时折叠。有一个标准的转换器。


当你读到我的回答时,很明显我对你正在做的事情了解得不够准确。


无论如何。


带有itemtemplate的组合框看起来像:

< DataTemplate> 
< StackPanel Orientation =" Horizo​​ntal">
< Image Fill =" {Binding SomeImageSource}"宽度= QUOT; 16"高度= QUOT; 16"余量= QUOT; 0,2,5,2" />
< TextBlock Text =" {Binding Name}" />
< / StackPanel>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>

数据触发器可能类似于:

< Image。 ....... 
< Style TargetType =" Image">
< Style.Triggers>
< DataTrigger Binding =" {Binding GotImage}"值= QUOT假QUOT;>
< Setter Property =" Visibility"值= QUOT;折叠" />
< / DataTrigger>
< /Style.Triggers>
< / Style>




Hello,

we have currently a Windows.Forms.Combobox derived from Windws.Form.Combobox.

This control shows for every entry in the combox

<dent(if > 0)><Picture(if exist)><Text (always)>

We override for this

protected override void OnDrawItem(DrawItemEventArgs e)

How can I achieve the same the WPF ?

tia

  Hendrik Schmieder

解决方案

A combobox is an itemscontrol.

That means you can bind itemssource to a collection of any object.

That object can have properties.

You can template each of them into an item in your combo.

.

I would define a class representing the data to go into each item.

Call that a ItemVM.

That would have public properties for each property you want to bind.

One is some sort of a picture.

You could expose a url for the picture or an actual picture and bind either to the source of an Image control.

Then you have some text.

That's a string property.

If you want to line these up you could use a grid with two columndefinitions.

Or maybe they're not supposed to line up, in which case you could use a stackpanel with orientation horizontal.

It seems you also don't want to show the picture based on some criteria which I don't totally follow.

You can use a datatrigger to drive that.

Set the Visibility property to Collapsed or Hidden.

You could either expose a visibility enumeration ( it's not just a bool in wpf ) from this or use a converter to convert from bool to collapsed when false. There's a standard converter.

As you read my response, it's obvious that I don't know quite enough about what you're doing to give a precise answer.

Anyhow.

A combobox with an itemtemplate looks something like:

                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Fill="{Binding SomeImageSource}" Width="16" Height="16" Margin="0,2,5,2" />
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

A datatrigger might look something like:

<Image ........                    
    <Style TargetType="Image">
        <Style.Triggers>
            <DataTrigger Binding="{Binding GotImage}" Value="false">
                <Setter Property="Visibility" Value="Collapsed"/>
             </DataTrigger>
        </Style.Triggers>
</Style>


这篇关于WPF组合框,带有图片,文字和标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆