WPF ComboBox - showing something different when no items are bound

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 13:47:58

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/885991/wpf-combobox-showing-something-different-when-no-items-are-bound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!