WPF ComboBox:如何使用带有绑定的通用ItemContainerStyle [英] WPF ComboBox: How to you utilise a generic ItemContainerStyle with binding

查看:148
本文介绍了WPF ComboBox:如何使用带有绑定的通用ItemContainerStyle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对ComboBoxItem内容使用通用样式,并将文本内容绑定到基础类的不同属性.因此,这是我能想到的最好的方法,但是绑定是硬编码的.因此,对于使用此ItemContainerStyle绑定到组合框的每个类,我都必须实现"MainText"和"SubText"属性.

I want to utilise a generic style for my ComboBoxItem content and have the text content bound to different properties on my underlying class. So this is the best I can come up with but the bindings are hard coded. So for every class bound to a combobox using this ItemContainerStyle I'd have to implement a "MainText" and "SubText" property.

问题是,有没有一种方法可以对绑定进行软编码,以便在从组合框引用的样式中,我可以指定使用基础类的哪些字符串属性.

Question is, is there a way to have the binding soft coded so where the style referenced from a combobox I can specify which string properties of the underlying class are used.

<Style TargetType="{x:Type ComboBoxItem}"  x:Key="ComboBoxItemStyleA1">
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="ComboBoxItem">

                    <Border x:Name="BB" Padding="8,3,8,3" Background="DarkGreen">
                        <StackPanel Margin="0">
                            <TextBlock Foreground="White"   FontSize="16" Text="{Binding MainText}"/>
                            <TextBlock Foreground="White"   FontSize="8" Text="{Binding SubText}"/>
                        </StackPanel>
                    </Border>
                        <ControlTemplate.Triggers>

                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" TargetName="BB" Value="#FF256294"/>
                            <Setter Property="Foreground" Value="White"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

要使用样式...

<ComboBox ItemsSource="{Binding Items}" 
    ItemContainerStyle="{StaticResource ComboBoxItemStyleA1}" />

还需要dowhilefor的答案(非常感谢-WPF非常棒,但是肯定是一次发现之旅)

Further to dowhilefor's answer (many many thanks - WPF is great but sure is a voyage of discovery)

我最初使用数据模板来定义单元格外观-然后想要使用基于comboboxitem的样式以及定义了控制模板的控件,可以在其中指定onmouseover触发器.即这些是要更改背景颜色等.

I used a data template to define the cell look originally - and then wanted to use a comboboxitem based style with a control template defined where I could specify the onmouseover triggers. i.e. these were to change the background color etc.

Butja)我无法删除上面模板的边框"部分-触发器由targettype ="BB"绑定到该模板.因此,我有点想将触发器绑定到容器,以便datatemplate将从模板绑定中获取背景,但不确定如何获取它.

Butj a) I couldn't remove the Border section of the template above - the triggers are tied to it by targettype="BB". so I kind of wanted to get the trigger bound to the container such that the datatemplate would pick up the background from the template binding but not sure how to get this plumbed in.

b)我意识到即使我注释掉触发器上特定于BB的绑定只是为了使其运行-组合框也找不到并使用我定义的DataTemplate.似乎在我的comboboxitemstyle中定义了controltemplate会阻止它拾取数据模板.

b) I realised that even if I comment out the BB specific bindings on the triggers just to get it to run - the combobox doesn't find and use the DataTemplate I defined. Seems that defining the controltemplate in my comboboxitemstyle stops it picking up the datatemplate.

我希望我在这里有道理-底线是我只想要一种可以与触发器配合使用的样式,即可设置我的cobobox项的背景颜色.它不应该知道数据是什么-即能够插入将(模板?)绑定到该背景颜色的数据模板.

I hope I make sense here - bottom line is I just want a style that I can apply with triggers in that set the background color of my cobobox item. It should not know what the data is - i.e. be able to plug in a datatemplate that will (template ?) bind to this background color.

非常感谢您的快速回复.

Many thanks for the very fast response.

顺便说一句,我将ItemContainerStyle与ItemTemplate结合使用,所以下拉列表中的组合可以与组合框列表中的显示不同

btw I'm using ItemContainerStyle in conjuction with ItemTemplate so I can have a different representation in the dropdown to what appears in the combobox list

推荐答案

首先不要为此使用 ItemContainerStyle .更准确地说,永远不要在 ItemContainerStyle 内对数据上下文有任何绑定,至少不要尝试.为什么?样式用于定义组合框项目的外观,而不考虑其内容.如果要定义内容的外观,则可以使用DataTemplate.有多种方法可以告诉组合框,他可以在哪里为您提供的数据找到合适的DataTemplate.检出属性 ItemTemplate ItemTemplateSelector 并搜索隐式样式,以查找有关它们的更多信息.

First of all don't use the ItemContainerStyle for that. To be more precise never have any Bindings to the datacontext inside an ItemContainerStyle, at least try not. Why? The Style is used for defining the appearance of a combobox item disregarding the content. If you want to define how the content should look like, you use a DataTemplate for that. There are multiple ways to tell the combobox where he can find a proper DataTemplate for the Data you supply. Checkout the property ItemTemplate, ItemTemplateSelector and search for implicit styles, to find out more about them.

对于您的问题,为组合框创建一个 ItemContainerStyle (如果确实需要),而不用担心将要放入的对象.现在,您仍然需要为每个人都提供多个DataTemplates,它们都具有您要模板化的数据对象的知识.没有办法解决,没有软数据绑定.只需尝试使模板小而简单即可.如果由于某种原因您需要使用完全相同的模板,但属性只是用不同的名称命名,为什么不对具有 Caption Description 属性的DataContext使用包装项,而您又可以在代码中决定如何用包裹在该对象中的真实数据填充这些属性.

So to your problem, create one ItemContainerStyle for you combobox (if you really have to anymore) which doesn't care about the object that will be put into. Now you still need to provide multiple DataTemplates each and everyone with the knowledge of the data object that you want to be templated. There is no way around it, there is no soft databinding. Just try to keep your templates small and simple. If for some reason you need the exact same template, but your properties are just named differently, why not use a wrapper item for the DataContext with the properties Caption, Description and you can decide in code how these properties are filled with your real data wrapped into this object.

这篇关于WPF ComboBox:如何使用带有绑定的通用ItemContainerStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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