如何仅在其源为空时禁用 RadComboxBox 并显示工具提示消息

How to disable RadComboxBox only when its source is Empty and display a tooltip message

我必须在 GridViewDataColumn 中添加一个 RadComboBox。此组合框必须填充一个列表。我必须提出一个条件,如果列表为空,则应禁用此组合框,并且必须显示一条工具提示消息 "No Settings available".

下面是我在 xaml 文件中的代码:

<telerikGrid:GridViewDataColumn HeaderCellStyle="{StaticResource HeaderStyle}"
                                                            Width="Auto"
                                                            Header="Decrypt" x:Name="colDecrypt">
    <telerikGrid:GridViewColumn.CellStyle>
        <Style TargetType="telerikGridView:GridViewCell">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="telerikGridView:GridViewCell">
                        <Border>
                            <telerik:RadComboBox x:Name="cbxDecrypt" Margin="5,1,5,1" Width="Auto"
                                            ItemsSource="{Binding Mode=OneWay, Source={StaticResource Parameter}, Path=EquivalenceNames}"
                                            SelectionChanged="cbxDecrypt_SelectionChanged" SelectedItem="{Binding SelectedEquivalence}"
                                            ToolTipService.ToolTip="No Decrypt Settings available"
                                            />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </telerikGrid:GridViewColumn.CellStyle>
</telerikGrid:GridViewDataColumn>

任何帮助将不胜感激。

我创建了一个简单的例子: 我们将使用 ObservableCollection 作为 DataContext:

DataContext = new ObservableCollection<string>();

默认情况下,工具提示不会显示在已禁用的控件上,因此我们需要使用 ToolTipService.ShowOnDisabled 属性。

<telerik:RadComboBox ToolTipService.ShowOnDisabled="True" ItemsSource="{Binding}">
    <telerik:RadComboBox.Style>
        <Style TargetType="telerik:RadComboBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Count, Mode=OneWay}" Value="0">
                    <Setter Property="IsEnabled" Value="False"/>
                    <Setter Property="ToolTip" Value="No Decrypt Settings available"/>
                </DataTrigger>    
            </Style.Triggers>
        </Style>
    </telerik:RadComboBox.Style>
</telerik:RadComboBox>

如果您使用 Noxaml 版本的 telerik 库,那么您需要一些修复,例如:

<Style TargetType="telerik:RadComboBox" BasedOn="{StaticResource RadComboBoxStyle}">
    <!--...-->
</Style>

我发现以下链接非常有用,可以轻松解决我的问题:

  1. 对于列表为空时禁用项目的第一个问题,这里提供了一个非常简单的解决方案:

ComboBox IsEnabled Binding Question in Silverlight Xaml

  1. 对于第二期显示tooltip消息,这里提供了一个很好的解决方案: http://dotplusnet.blogspot.in/2010/09/workaround-for-showing-tooltip-for.html

使用 Border Background="Transparent" 可以很好地解决这个问题。