无法处理 UWP 中的 MyToolKit.Extended DataGrid

Unable to handle MyToolKit.Extended DataGrid in UWP

我正在开发 UWP 应用程序。 在这里,我想以 table 格式显示数据,如下所示,

我的目的是搜索项目并将它们添加到此网格中。 网格中没有数据 如果我单击 圆形按钮(红色,它是一个数据网格 Header 模板) 它工作正常,但是如果网格包含一些数据,那么如果我单击在圆形图像按钮上出现 unhand-led 异常,如 ,

这里放上我用过的代码,

<controls:DataGrid x:Name="dgNewBill" Grid.Row="0" Grid.Column="0" MinHeight="500" HorizontalAlignment="Left" HeaderBackground="Black"  DefaultOrderIndex="1" Background="Black" RowBackgroundEvenBrush="Black" RowBackgroundOddBrush="Black" >
                                <controls:DataGrid.RowStyle>
                                    <Style TargetType="ListBoxItem">
                                        <Setter Property="Padding" Value="0"></Setter>
                                        <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
                                    </Style>
                                </controls:DataGrid.RowStyle>

                                <controls:DataGrid.Columns>
                                    <controls:DataGridTextColumn Width="50" Binding="{Binding SNumber}" Foreground="White"
                     IsAscendingDefault="False" CanSort="False">
                                        <controls:DataGridTextColumn.Header>
                                            <Button x:Uid="RSNO" Height="40"  Width="50" HorizontalAlignment="Left" Style="{StaticResource RoundButtonTemplate}" Margin="-10,0,0,0"/>
                                        </controls:DataGridTextColumn.Header>
                                        <controls:DataGridTextColumn.Style>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="TextAlignment" Value="Center"></Setter>
                                            </Style>
                                        </controls:DataGridTextColumn.Style>
                                    </controls:DataGridTextColumn>

                                    <!--Item Column-->
                                    <controls:DataGridTextColumn Width="380" Binding="{Binding description}" Foreground="White" CanSort="False">
                                        <controls:DataGridTextColumn.Header>
                                            <Button x:Uid="RItem"  Height="40" Width="380"   Style="{StaticResource RoundButtonTemplate}"  Margin="-12,0,0,0"/>
                                        </controls:DataGridTextColumn.Header>
                                        <controls:DataGridTextColumn.Style>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="TextAlignment" Value="Center"></Setter>
                                            </Style>
                                        </controls:DataGridTextColumn.Style>
                                    </controls:DataGridTextColumn>
                                    <!--UOM column-->
                                    <controls:DataGridTextColumn Width="120" Binding="{Binding uom}" Foreground="White" CanSort="False">
                                        <controls:DataGridTextColumn.Header>
                                            <Button x:Uid="RUOM" Height="40" Width="120"  Style="{StaticResource RoundButtonTemplate}"  Margin="-12,0,0,0"/>
                                        </controls:DataGridTextColumn.Header>
                                        <controls:DataGridTextColumn.Style>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="TextAlignment" Value="Center"></Setter>
                                            </Style>
                                        </controls:DataGridTextColumn.Style>
                                    </controls:DataGridTextColumn>
                                    <!--Price Column-->
                                    <controls:DataGridTemplatedColumn>
                                        <controls:DataGridTemplatedColumn.Header>
                                            <Button x:Uid="RPrice" Height="40" Width="110"  Style="{StaticResource RoundButtonTemplate}"  Margin="-12,0,0,0"/>
                                        </controls:DataGridTemplatedColumn.Header>
                                        <controls:DataGridTemplatedColumn.CellTemplate>
                                            <DataTemplate>
                                                <StackPanel Name="pricePanel" Width="110" Height="30" Tapped="pricePanel_Tapped" >
                                                    <TextBlock Name="price" Foreground="White"  Text='{Binding price}' VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,5,0,0"></TextBlock>
                                                </StackPanel>
                                            </DataTemplate>
                                        </controls:DataGridTemplatedColumn.CellTemplate>
                                    </controls:DataGridTemplatedColumn>


                                     <!--Qty Column-->
                                    <controls:DataGridTemplatedColumn>
                                        <controls:DataGridTemplatedColumn.Header>
                                            <Button x:Uid="RQty" Height="40" Width="110"  Style="{StaticResource RoundButtonTemplate}"  Margin="-25,0,0,0"/>
                                        </controls:DataGridTemplatedColumn.Header>
                                        <controls:DataGridTemplatedColumn.CellTemplate>
                                            <DataTemplate>
                                                <StackPanel Name="quantityPanel" Width="50" Height="30" Tapped="quantityPanel_Tapped">
                                                    <TextBlock Name="quantity" Foreground="White"  Text='{Binding quantity}' VerticalAlignment="Center" Margin="0,5,0,0"></TextBlock>
                                                </StackPanel>
                                            </DataTemplate>
                                        </controls:DataGridTemplatedColumn.CellTemplate>
                                    </controls:DataGridTemplatedColumn>

                                    <!--Discount Column-->
                                    <controls:DataGridTemplatedColumn>
                                        <controls:DataGridTemplatedColumn.Header>
                                            <Button x:Uid="Rdiscount" Height="40" Width="60"  Style="{StaticResource RoundButtonTemplate}"  Margin="-25,0,0,0"/>
                                        </controls:DataGridTemplatedColumn.Header>
                                        <controls:DataGridTemplatedColumn.CellTemplate>
                                            <DataTemplate>
                                                <StackPanel Name="discountPanel" Width="60" Height="30" Tapped="discountPanel_Tapped" >
                                                    <TextBlock Name="Discount" Foreground="White" Text='{Binding discount}' VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,5,0,0"></TextBlock>
                                                </StackPanel>
                                            </DataTemplate>
                                        </controls:DataGridTemplatedColumn.CellTemplate>
                                    </controls:DataGridTemplatedColumn>

                                    <!--Cost Column-->
                                    <controls:DataGridTextColumn Width="110" Binding="{Binding cartTotal}" Foreground="White" CanSort="False" >
                                        <controls:DataGridTextColumn.Header>
                                            <Button x:Uid="RCartTotal" Height="40" Width="110"   Style="{StaticResource RoundButtonTemplate}"  Margin="-25,0,0,0"/>
                                        </controls:DataGridTextColumn.Header>
                                        <controls:DataGridTextColumn.Style>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="TextAlignment" Value="Center"></Setter>
                                            </Style>
                                        </controls:DataGridTextColumn.Style>
                                    </controls:DataGridTextColumn>

                                <!--void image column-->
                                    <controls:DataGridTemplatedColumn>
                                        <controls:DataGridTemplatedColumn.Header>
                                            <Button Name="Cancelbtn" Height="50" Click="Cancelbtn_Click" Width="55" >
                                                <Image Source="/Images/erase.png"  Name="ImgClearCartp" Height="40" Width="40" ></Image>
                                            </Button>
                                        </controls:DataGridTemplatedColumn.Header>
                                        <controls:DataGridTemplatedColumn.CellTemplate>
                                            <DataTemplate>
                                                <StackPanel Name="voidImagePanel" Width="50" Height="30" Tapped="voidImagePanel_Tapped" >
                                                    <Image Name="VoidImage" Source='{Binding imageUrl}' Height="20" Width="30" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0,5,0,0"></Image>
                                                </StackPanel>
                                            </DataTemplate>
                                        </controls:DataGridTemplatedColumn.CellTemplate>
                                    </controls:DataGridTemplatedColumn>
                                </controls:DataGrid.Columns>
                            </controls:DataGrid>

我已经测试过,根据我的理解,因为使用 DataGridTemplatedColumn 得到这个异常。但根据我的范围,我必须使用 TemplateColumn 而不是 DataGridTextColumn.

而且我用过https://github.com/MyToolkit/MyToolkit/wiki/DataGrid

link。 请建议我如何解决这个问题?

I have tested and according to my understanding, because of using DataGridTemplatedColumn getting this exception. But according to my scope I have to use TemplateColumn instead of DataGridTextColumn.

是的,对于这个问题,一种解决方法是将 DataGridTemplatedColumn 替换为 DataGridTextColumn。但是您可以使用其他两种解决方法,我认为这比直接替换更好。

  • 您可以为 DataGridTemplatedColumn 添加 order 属性,例如 Order="{Binding imageUrl}",如下所示:

    <controls:DataGridTemplatedColumn Order="{Binding imageUrl}">
    <controls:DataGridTemplatedColumn.Header>
        <Button Name="Cancelbtn"
                Width="55"
                Height="50"
                Background="Green"
                Click="Cancelbtn_Click">
            <Image Name="ImgClearCartp"
                   Width="40"
                   Height="40"
                   Source="Assets/cafee2.jpg" />
        </Button>
    </controls:DataGridTemplatedColumn.Header>
    <controls:DataGridTemplatedColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Name="voidImagePanel"
                        Width="50"
                        Height="30"
                        Tapped="voidImagePanel_Tapped">
                <Image Name="VoidImage"
                       Width="30"
                       Height="20"
                       Margin="0,5,0,0"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Center"
                       Source="{Binding imageUrl}" />
            </StackPanel>
        </DataTemplate>
    </controls:DataGridTemplatedColumn.CellTemplate>
    
  • 如果您不需要对列进行排序,您可以简单地为 DataGridTemplatedColumn 元素添加 CanSort="False",如下所示:

    <controls:DataGridTemplatedColumn  CanSort="False">
    ...
    </controls:DataGridTemplatedColumn.CellTemplate>
    

    MyToolkit 异常的原因是 DataGridTemplatedColumn 元素的 order 属性 初始为空,但要从中获取 path 参数,如下图 shows.You也可以将此问题提交给MyToolKit issues请求MyToolKit团队修复。