datagrid为空时显示消息

时间:2012-10-09 06:52:50

标签: wpf xaml datatrigger xceed-datagrid xceed

当数据网格中没有显示任何项目时,我想在数据网格中显示一条简单的消息。现在我有以下XAML代码:

 <xcdg:DataGridControl x:Name="lsvSleutels"
                          ItemsSourceName="Registratiesleutels"
                          ItemsSource="{Binding Source={StaticResource DataViewSource}}"
                          Style="{StaticResource SmbDataGridControlStyle}"
                          IsRefreshCommandEnabled="False"
                          rf:XceedGridService.SettingsKeyName="RegistratieSleutelOverzichtGridKey"
                          rf:XceedGridService.IsGroupingEnabled="True"
                          rf:XceedGridService.ItemContextMenu="{StaticResource ItemContextMenu}"
                          rf:XceedGridService.ItemsContextMenu="{StaticResource ItemsContextMenu}"
                          rf:XceedGridService.GroupBarVisibility="Collapsed"
                          rf:XceedGridService.LoadUserSettings="True"  >
          *columns and stuff*
    </xcdg:DataGridControl>

        <TextBlock x:Name="txtNoDataFound"
                   FontSize="18"
                   Foreground="Blue"
                   Text="No items found"
                   Margin="10,25,20,10"
                   VerticalAlignment="Top"
                   HorizontalAlignment="Center"
                   Visibility="Hidden">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=lsvSleutels, Path=Items.Count}"
                                     Value="0">
                            <Setter Property="Visibility"
                                    Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>

然而,消息未显示。我的第一个猜测是DataTrigger有问题,但我不知道是什么。有任何想法吗?提前谢谢!

3 个答案:

答案 0 :(得分:0)

以下是包含列表框控件的示例代码:

 <ListBox x:Name="lsvSleutels">
                <ListBoxItem>asdasdasd</ListBoxItem>
                <ListBoxItem>asdasdasd</ListBoxItem>
                <ListBoxItem>asdasdasd</ListBoxItem>
                <ListBoxItem>asdasdasd</ListBoxItem>
                <ListBoxItem>asdasdasd</ListBoxItem>
                <ListBoxItem>asdasdasd</ListBoxItem>
                <ListBoxItem>asdasdasd</ListBoxItem>
                <ListBox.Style>
                    <Style TargetType="{x:Type ListBox}">
                        <Style.Setters>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListBox">
                                        <Grid>
                                            <ItemsPresenter/>
                                            <TextBlock x:Name="txtNoDataFound"
                                                   FontSize="18"
                                                   Foreground="Blue"
                                                   Text="No items found"
                                                   Margin="10,25,20,10"
                                                   VerticalAlignment="Top"
                                                   HorizontalAlignment="Center"
                                                   Visibility="Hidden">

                                            </TextBlock>
                                        </Grid>
                                        <ControlTemplate.Triggers>
                                            <DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}" Value="0">
                                                <Setter TargetName="txtNoDataFound" Property="Visibility" Value="Visible" />
                                            </DataTrigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style.Setters>
                    </Style>
                </ListBox.Style>
            </ListBox>

希望这会对你有所帮助。

答案 1 :(得分:0)

由于WPF属性层次结构的工作方式,这种情况正在发生。在根级别设置的属性不能被样式覆盖。

您需要从您的文本框声明中取出visibility =“Hidden”属性,并将其移动到样式中的setter。

像这样:

<TextBlock x:Name="txtNoDataFound"
               FontSize="18"
               Foreground="Blue"
               Text="No items found"
               Margin="10,25,20,10"
               VerticalAlignment="Top"
               HorizontalAlignment="Center">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Visibility" Value="Hidden" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=lsvSleutels, Path=Items.Count}"
                                 Value="0">
                        <Setter Property="Visibility"
                                Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>a

答案 2 :(得分:0)

在XAML中添加InvertBoolToVisibility转换器:

 <utils:InvertBoolToVisibilityConverter x:Key="BoolToVisibility" />

在C#中:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var hasItems = (bool) value;

        return hasItems ? Visibility.Collapsed : Visibility.Visible;
    }

向ControlTemplate添加行:

<Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGrid}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2" Grid.Row="1"/>
                                        <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                        <Grid Grid.Column="1" Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>                                            
                                        </Grid>
                                        <!--<TextBlock Grid.ColumnSpan="2" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FFD9DEE3" Text="No Data Shown" Visibility="{Binding HasItems, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Converter={StaticResource BoolToVisibility}}" />-->
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>