我正在开发 UWP 应用程序。 在这里,我想在下面的表格格式中显示数据,
我的目的是搜索项目并将其添加到此网格中。 没有网格中的数据如果我点击圆形按钮(红色它'数据网格标题模板)它的工作正常,但如果网格包含一些数据,如果我点击圆形图像按钮得到像Unhand一样的例外,
这里我放置了我用过的代码,
<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
链接。 请建议我如何解决此问题?
答案 0 :(得分:1)
我已经测试并根据我的理解,因为使用DataGridTemplatedColumn获取此异常。但根据我的范围,我必须使用TemplateColumn而不是DataGridTextColumn。
是的,对于此问题,一种解决方法是将DataGridTemplatedColumn
替换为DataGridTextColumn
。但是您可以使用其他两种解决方法,我认为这比直接替换更好。
您可以为order
添加Order="{Binding imageUrl}"
DataGridTemplatedColumn
属性,如下所示:
<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>
如果您不需要对列进行排序,只需为CanSort="False"
元素添加DataGridTemplatedColumn
,如下所示:
<controls:DataGridTemplatedColumn CanSort="False">
...
</controls:DataGridTemplatedColumn.CellTemplate>
MyToolkit异常的原因是order
元素的DataGridTemplatedColumn
属性为null,但是从它获得path
参数如下图所示。您也可以提交此向MyToolKit issues发出问题,要求MyToolKit小组修复它。