当DataGrid中不存在任何数据时,将显示默认类名

时间:2011-08-23 22:14:38

标签: c# wpf xaml

我是WPF的新手。以下代码段具有DataGrid的样式/设置。

                                                                

        <!--Global View Model Locator-->
        <vm:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True" />

        <!-- Main menu style -->
        <Style x:Key="MainMenu" TargetType="{x:Type MenuItem}">
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="FontSize" Value="14"/>
        </Style>

        <!-- Data grid cell style -->
        <Style x:Key="CenterCellStyle" TargetType="DataGridCell">
            <Style.Setters>
                <Setter Property="HorizontalAlignment" Value="Center"/>
            </Style.Setters>
        </Style>

        <!-- DataGridColumnHeader style -->
        <Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">                                
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                        <Grid>
                            <dg:DataGridHeaderBorder x:Name="HeaderBorder" BorderThickness="0"
                                                     Padding="0" SeparatorVisibility="Hidden">
                                <Border BorderThickness="0">
                                    <TextBlock Text="{Binding}" 
                                               VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                               HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>                                        
                                </Border>
                            </dg:DataGridHeaderBorder>                                                                   
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>                
        </Style>

        <!-- CENTER aligned DataGridColumnHeader style -->
        <Style x:Key="CenterColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}" 
               BasedOn="{StaticResource ColumnHeaderStyle}">                
                <Setter Property="HorizontalContentAlignment" Value="Center"/>                
        </Style>

        <!-- RIGHT aligned DataGridColumnHeader style -->            
        <Style x:Key="RightColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}" 
               BasedOn="{StaticResource ColumnHeaderStyle}">
            <Setter Property="HorizontalContentAlignment" Value="Right"/>
        </Style>            

        <!-- Consensus DataGrid -->
        <Style x:Key="ConsensusDataGridStyle" TargetType="{x:Type DataGrid}">
            <Style.Setters>                    
                <Setter Property="ColumnHeaderStyle" Value="{StaticResource CenterColumnHeaderStyle}"/>                    
            </Style.Setters>
        </Style>

        <!-- Validation Error Template -->
        <DataTemplate DataType="{x:Type ValidationError}">
            <TextBlock FontStyle="Italic" Foreground="Red"
                       HorizontalAlignment="Left" Margin="5,5" Text="{Binding Path=ErrorContent}"/>
        </DataTemplate>
    </ResourceDictionary>
</Application.Resources>

相应的XAML:

<UserControl x:Class="Viewer.Views.ConsensusReadControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <DataGrid x:Name="dtGridReads"  AutoGenerateColumns="False" 
            VirtualizingStackPanel.IsVirtualizing="True"                                       
            VirtualizingStackPanel.VirtualizationMode ="Standard"
              EnableColumnVirtualization="True"
              EnableRowVirtualization="True"
            ScrollViewer.IsDeferredScrollingEnabled="True"
            CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="True"
             ItemsSource ="{Binding}" Block.TextAlignment="Center"
             AlternatingRowBackground="LightGoldenrodYellow" RowBackground="White"
              CanUserAddRows="False" CanUserDeleteRows="False" FrozenColumnCount="1"
               GridLinesVisibility="None" Style="{StaticResource ConsensusDataGridStyle}" FontSize="12" >
    </DataGrid>
</UserControl>

在将数据填充到DataGrid之前,DataGrid会显示“Viewer.Views.MainViewModel”。文本居中。是否有类名称输出到屏幕的原因?感谢。

1 个答案:

答案 0 :(得分:0)

那是因为你在

中绑定了整个视图模型
ItemsSource="{Binding}"

您可能希望绑定视图模型类的(可观察的)集合属性。