大佬教程收集整理的这篇文章主要介绍了.net – 如何在Silverlight的组合框中为选定和下拉状态使用不同的模板?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想我可以通过修改ComboBox的ItemTemplate和ItemContainerStyle来实现这一点. ItemContainerStyle包含以下ContentPresenter:
<ContentPresenter HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplatE}"/>
所以我假设我可以在这里设置ContentTemplate,它会起作用.但我似乎无法让它工作:
<DataTemplate x:Key="ComboBoxDataTemplate"> <Grid> <TextBlock Text="{Binding Path='Name'}"/> </Grid> </DataTemplate> <DataTemplate x:Key="ComboBoxItemTemplate"> <StackPanel> <TextBlock Text="{Binding Path='Name'}"/> <Image source="{Binding Path='source'}" Width="64" Height="64"/> </StackPanel> </DataTemplate> <Style x:Key="ComboBoxItemStyle1" TargetType="ComboBoxItem"> ... <Setter Property="ContentTemplate" Value="{Staticresource ComboBoxItemTemplatE}"/> ...
这是我的组合框:
<ComboBox Width="70" Margin="3,0" Itemssource="{Staticresource sourcE}" ItemTemplate="{Staticresource ComboBoxDataTemplatE}" ItemContainerStyle="{Staticresource ComboBoxItemStyle1}" />
我能让它工作的唯一方法是从ItemContainerStyle中删除ContentPresenter,并将其替换为我的自定义模板(ComboBoxItemTemplatE)的内容.但我不认为@R_961_10675@用这种方法,因为它意味着ContentPresenter不再存在(ComboBox中的代码可能依赖于它存在).
任何有关显示具有不同下拉列表和选定模板的组合框的帮助将不胜感激!
正如Jobi指出的那样,您可以尝试仅使用自定义样式.如果您始终知道内容的类型,则可以安全地排除ContentPresenter. ContentPresenter只允许您使用DataTemplate显示一些随机数据.但你可以用TextBlock和Image替换它.您只是失去了指定DataTemplate的能力.
Jobi方法的问题在于选择项目不会显示它的图像,即使它在下拉列表中也是如此.实际上,所选项目显示在两个位置(ComboBox的下拉列表和主体).在一个位置,您需要一个DataTemplate,并且在另一个位置需要不同的DataTemplate.
你最好的选择是重新组合ComboBox.您可以从here获取默认样式.有一个名为“ContentPresenter”的ContentPresenter.你需要:
>删除/更改ContentPresenter的名称,因此ComboBox不会自动设置Content / ContentTemplate属性
>绑定ContentPresenter.Content属性,如下所示:“{TemplateBinding SELEctedObject}”
>将ContentPresenter.ContentTemplate属性设置为不带Image的DataTemplate
>使用Image和TextBlock将ComboBox.ItemTemplate属性设置为DataTemplate
>为ComboBox样式指定一个显式键,如x:Key =“MyComboBoxStyle”
>使用COR_412_11845@boBox上的Style,如Style =“{Staticresource MyComboBoxStylE}”
当在ComboBox的主体中显示所选项时,这会有效地忽略ComboBoxItem.ContentTemplate,但在下拉列表中显示ComboBoxItem时会使用它.
以上是大佬教程为你收集整理的.net – 如何在Silverlight的组合框中为选定和下拉状态使用不同的模板?全部内容,希望文章能够帮你解决.net – 如何在Silverlight的组合框中为选定和下拉状态使用不同的模板?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。