微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何基于ComboBox ControlTemplate的TemplatedParent的ItemSource的属性设置属性?

如何解决如何基于ComboBox ControlTemplate的TemplatedParent的ItemSource的属性设置属性?

我的ComboBox一个DataTemplate。我指的是这个 Can I use a different Template for the selected item in a WPF ComboBox than for the items in the dropdown part?,因为我的要求与此类似:

<ComboBox x:Name="MyComboBox" ItemsSource="{Binding Items}"
                    Style="{StaticResource ComboBoxStyle}"
                    Template="{StaticResource ComboBoxControlTemplate}"
                    ItemTemplateSelector="{UI:ComboBoxTemplateSelector
                    SelectedItemTemplate={StaticResource ComboBoxSelectedItemDataTemplate},DropdownItemstemplate={StaticResource ComboBoxDataTemplate}}"/>

效果很好,但我又提出了一个要求:基于ComboBox设置ToggleButton的{​​{1}}的{​​{1}}。因此,我在设计器页面上右键单击此Background,选择

编辑模板->编辑副本

生成自动模板。在自动生成的模板中,我得到:

SelectedItem

所以我要在上面的代码中更改的是ComboBox。我希望<ControlTemplate x:Key="ComboBoxControlTemplate" targettype="{x:Type ComboBox}"> <ToggleButton> <Border x:Name="splitBorder" Background="{TemplateBinding Background}" ...> <Path x:Name="Arrow" .../> </Border> </ToggleButton> </ControlTemplate> 基于称为Background="{TemplateBinding Background}" ItemSource viewmodel属性,就像在Background中一样,

Selectedname

搜索了很多,但不确定应该怎么做。请帮忙。谢谢。

解决方法

设置Background的{​​{1}}属性与设置ComboBox的{​​{1}}类似:

Background

以上内容将TextBlock属性绑定到<ComboBox x:Name="MyComboBox" ... Background="{Binding SelectedItem.SelectedName,RelativeSource={RelativeSource Self},Converter={StaticResource StringToColorConverter}}"> 当前选定项的Background属性。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。