开发者

Default class name showing up when no data is present in DataGrid

开发者 https://www.devze.com 2023-03-30 10:39 出处:网络
I\'m totally new to WPF.The following code snippet has the style/settings for a DataGrid. <!--Global View Model Locator-->

I'm totally new to WPF. The following code snippet has the style/settings for a 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="VerticalContentA开发者_如何学Clignment" 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>

The corresponding 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>

Before the data gets populated into the DataGrid, the DataGrid says "Viewer.Views.MainViewModel". The text is centered. Is there a reason why the class name is getting output to the screen? Thanks.


That's because you are binding to the entire view model in

ItemsSource="{Binding}"

You probably want to bind or a (observeable) collection property of your view model class.

0

精彩评论

暂无评论...
验证码 换一张
取 消