当 DataGrid 中不存在数据时显示默认类名

发布于 2024-11-30 23:27:38 字数 5182 浏览 0 评论 0原文

我对 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”。文本居中。类名输出到屏幕上是否有原因?谢谢。

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="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>

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2024-12-07 23:27:38

这是因为您正在绑定到整个视图模型,

ItemsSource="{Binding}"

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文