WPF ComboBox - 当没有项被绑定时显示不同的东西 [英] WPF ComboBox - showing something different when no items are bound

查看:236
本文介绍了WPF ComboBox - 当没有项被绑定时显示不同的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ComboBox,我想改变它的外观,当ItemsSource属性为null。当它处于这种状态时,我想显示一个文本检索数据的TextPanel,并给它一个类似于水印文本框的外观。



我想要做到这一点,我需要一个ControlTemplate,和一个触发器。我有这里的ControlTemplate:

 < ControlTemplate x:Key =LoadingComboTemplateTargetType = {x:Type ComboBox}> 
< Grid>
< TextBlock x:Name =textBlockOpacity =0.345Text =正在检索数据...Visibility =Hidden/>
< / Grid>
<! -
< ControlTemplate.Triggers>
< Trigger Property =ComboBox.ItemsSourceValue =0>
< Setter Property =VisibilityValue =Visible/>
< / Trigger>
< /ControlTemplate.Triggers>
- >
< / ControlTemplate>

但是我的问题是当ItemsSource属性为null时如何设置触发器来显示?我已经尝试了几种不同的方式,每种方式给我错误消息值'ItemsSource'不能分配到属性属性。无效的PropertyDescriptor值。我的ComboBox xaml是这个(包括尝试的触发器):

 < ComboBox Margin =112,35, 80,0
Name =MyComboBox
Height =22.723
VerticalAlignment =Top
DisplayMemberPath =FriendlyName
SelectedValuePath =Path
TabIndex =160
>
< Trigger>
< Condition Property =ItemsSourceValue =0/>
< Setter Property =TemplateValue ={StaticResource LoadingComboTemplate}/>
< / Trigger>
< / ComboBox>

现在触发器应该放在ComboBox还是ControlTemplate?如何访问ComboBox的ItemsSource属性?

解决方案

尝试将 {x:Null} 替换为条件的值,而不是0.



 < Style TargetType =ComboBoxx:K​​ey =LoadingComboStyle> 
< Style.Triggers>
< Trigger Property =ItemsSourceValue ={x:Null}>
< Setter Property =TemplateValue ={StaticResource LoadingComboTemplate}/>
< / Trigger>
< / Style.Triggers>
< / Style>

< ComboBox Style ={StaticResource LoadingComboStyle}...>

它只适用于一个样式的原因是,在触发器集合中只允许EventTriggers框架元素。对于属性触发器(如上所示),您需要使用一种样式(我每天都学习一些东西)。



请参阅 FrameworkElement.Triggers


请注意,在元素上建立的触发器集合仅支持EventTrigger,而不支持属性触发器(Trigger)。如果需要属性触发器,则必须将它们放在样式或模板中,然后直接通过Style属性将该样式或模板分配给元素,或通过隐式样式引用间接分配。



I have a ComboBox, and i want to change its look when the ItemsSource property is null. When it is in that state, i want to show a TextPanel with the text "Retrieving data" in it, and give it a look kind of similar to the watermarked textbox.

I figure to do this i need a ControlTemplate, and a trigger. I have the ControlTemplate here:

<ControlTemplate x:Key="LoadingComboTemplate" TargetType="{x:Type ComboBox}">  
    <Grid>  
        <TextBlock x:Name="textBlock" Opacity="0.345" Text="Retrieving data..." Visibility="Hidden" />  
    </Grid>  
    <!--  
    <ControlTemplate.Triggers>  
        <Trigger Property="ComboBox.ItemsSource" Value="0">  
            <Setter Property="Visibility" Value="Visible" />  
        </Trigger>  
    </ControlTemplate.Triggers>  
    -->  
</ControlTemplate>  

but my issue is how do i set up the trigger to show this when the ItemsSource property is null? I have tried a couple of different ways, and each way has given me the error message "Value 'ItemsSource' cannot be assigned to property 'Property'. Invalid PropertyDescriptor value.". My ComboBox xaml is this (including the attempted trigger):

<ComboBox Margin="112,35,80,0"   
      Name="MyComboBox"   
      Height="22.723"   
      VerticalAlignment="Top"   
      DisplayMemberPath="FriendlyName"   
      SelectedValuePath="Path"   
      TabIndex="160"   
      >  
    <Trigger>  
        <Condition Property="ItemsSource" Value="0" />  
        <Setter Property="Template" Value="{StaticResource LoadingComboTemplate}" />  
    </Trigger>    
</ComboBox>  

now should the trigger go on the ComboBox, or on the ControlTemplate? How do i access the ComboBox's ItemsSource property? Should i even be using a trigger?

Thanks!

解决方案

Try putting {x:Null} for the value of the condition instead of 0.

Also I got it working by moving the Trigger to a style and modifing it slightly, see below.

<Style TargetType="ComboBox" x:Key="LoadingComboStyle">
    <Style.Triggers>
        <Trigger Property="ItemsSource" Value="{x:Null}">
            <Setter Property="Template" Value="{StaticResource LoadingComboTemplate}" />
        </Trigger>
    </Style.Triggers>
</Style>

<ComboBox Style="{StaticResource LoadingComboStyle}" .... >

The reason it only works in a style, is that only EventTriggers are allowed in the triggers collection directly on the Framework Element. For property triggers (like above) you need to use a style (I learn something every day).

See FrameworkElement.Triggers

Note that the collection of triggers established on an element only supports EventTrigger, not property triggers (Trigger). If you require property triggers, you must place these within a style or template and then assign that style or template to the element either directly through the Style property, or indirectly through an implicit style reference.

这篇关于WPF ComboBox - 当没有项被绑定时显示不同的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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