WPF .Net FameWork4.6.1 DataGrid 数据要点击才显示

DataGrid 数据要点击才显示然后 点击其他地方又隐藏了

是照着微软例子写的:https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/getting-started/walkthrough-my-first-wpf-desktop-application
我的项目源代码:

链接:https://pan.baidu.com/s/1EAu-NoczTAPpOdTadHQR_Q

提取码:fntw

如图:数据有的,就是不显示:

图片说明
图片说明

DataGrid 代码:
图片说明

整个文件:
<Page x:Class="ExpenseIt.ExpenseReportPage"
      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" 
      xmlns:local="clr-namespace:ExpenseIt"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="ExpenseIt - View Expense">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="watermark.png"/>
        </Grid.Background>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="230"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label Grid.Column="1" Style="{StaticResource headerTextStyle}">
            Expen Report For:
        </Label>
        <Grid Margin="10" Grid.Column="1" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition />
            </Grid.RowDefinitions>
            <StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Orientation="Horizontal" >
                <Label Style="{StaticResource labelStyle}">Name:</Label>
                <Label Style="{StaticResource labelStyle}" Content="{Binding XPath=@Name}"></Label>
            </StackPanel>
            <!-- DepartMent -->
                <StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Orientation="Horizontal">
                <Label Style="{StaticResource labelStyle}">DeparTment</Label>
                <Label Style="{StaticResource labelStyle}" Content="{Binding XPath=@Department}"></Label>
            </StackPanel>
            <Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top"
                  HorizontalAlignment="Left">
                <!--Templates to Diaplay Expense Report data-->
                <Grid.Resources>
                    <!--Reason item Template-->
                    <DataTemplate x:Key="typeItemTemplate">
                        <Label Content="{Binding XPath=@ExpenseType}"/>
                    </DataTemplate>
                    <!--Amount item template-->
                    <DataTemplate x:Key="amountItemTemplate">
                        <Label Content="{Binding XPath=@ExpenseAmount}"/>
                    </DataTemplate>
                </Grid.Resources>
                <!-- Expense Type and Amount table -->
                <DataGrid ItemsSource="{Binding XPath=Expense}" ColumnHeaderStyle="{StaticResource columnHeaderSyle}"
                    AutoGenerateColumns="False" RowHeaderWidth="0" RowHeight="50">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="ExpenseType" CellEditingTemplate="{StaticResource typeItemTemplate}"/>
                        <DataGridTemplateColumn Header="Amount" CellEditingTemplate="{StaticResource amountItemTemplate}"/>
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>
        </Grid>
    </Grid>
</Page>

1个回答

图片说明
换成CellTemplete就可以了,CellEditingTemplete只有在“编辑”状态的时候才生效,你点击的时候控件的状态变为”编辑“状态,所以点击的时候可见

tiankong1213
tiankong1213 万分感谢
8 个月之前 回复
tiankong1213
tiankong1213 。。。看来我还不够班
8 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!