当 DataGrid 中不存在数据时显示默认类名
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您正在绑定到整个视图模型,
您可能想要绑定视图模型类的(可观察的)集合属性。
That's because you are binding to the entire view model in
You probably want to bind or a (observeable) collection property of your view model class.