我对 WPF 比较陌生,并且正在尝试更改边界值为十进制零的某些网格单元。目的是在单元格有 0 的地方将单元格留空。例如,如果 TranDeposit 列为零,我想将其更改为空白。在 ASP.NET 中,这很可能通过使用 DataBound 事件来实现。XAML 代码如下:
<DataGrid x:Name="transactionsDataGrid" Grid.Row="3" AutoGenerateColumns="False" EnableRowVirtualization="True"
ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected" RowHeaderWidth="0" Padding="10,0,0,0"
AlternatingRowBackground="#FF888888" GridLinesVisibility="Horizontal" SelectionUnit="FullRow" RowBackground="#FFCCCCCC" VerticalAlignment="Top" SelectedIndex="0"
HeadersVisibility="Column" Background="#FF292929" Height="355" SelectionChanged="TransactionsDataGrid_SelectionChanged" CanUserAddRows="false" Margin="5,0,5,0"
Loaded="DataGrid_RowLoaded">
<DataGrid.Columns>
<DataGridTextColumn x:Name="tranDateColumn" Binding="{Binding TranDate, StringFormat=MM/dd/yyyy}" Header="Date" Width="90" IsReadOnly="True"/>
<DataGridTextColumn x:Name="tranDescriptionColumn" Binding="{Binding TranDescription}" Header="Description" Width="*" IsReadOnly="True"/>
<DataGridTextColumn x:Name="tranCategoryColumn" Binding="{Binding TranCategory}" Header="Category" Width="340" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<!--<Setter Property="HorizontalAlignment" Value="Right" />-->
<Setter Property="ToolTip" Value="{Binding Path=TranCategory}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn x:Name="tranNotesColumn" Binding="{Binding TranNotes}" Header="Notes" Width="120" IsReadOnly="True"/>
<DataGridCheckBoxColumn x:Name="tranTaxColumn" Binding="{Binding TranTax}" Header="Tax" Width="SizeToHeader" IsReadOnly="True"/>
<DataGridCheckBoxColumn x:Name="tranClearedColumn" Binding="{Binding TranCleared}" Header="Cleared" Width="SizeToHeader" IsReadOnly="True" />
<DataGridTextColumn x:Name="tranDepositColumn" Binding="{Binding TranDeposit}" Header="Deposit" Width="80" IsReadOnly="True"/>
<DataGridTextColumn x:Name="tranWithdrawlColumn" Binding="{Binding TranWithdrawl}" Header="Withdrawal" Width="80" IsReadOnly="True" />
<DataGridTextColumn x:Name="tranBalanceColumn" Binding="{Binding TranBalance}" Header="Balance" Width="80" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
如您所见,我尝试使用Loaded事件来访问该行,但是,没有可用于动态更改此事件的行。我尝试了LoadingRow事件,但是,当尝试使用非常流行的数据网格帮助程序扩展“DataGridHelper”时,我在 GetCell 方法中得到了 NullReferenceException:
presenter = GetVisualChild<DataGridCellsPresenter>(row);
行值正确,但演示者值为空。
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
if (presenter == null)
{
grid.ScrollIntoView(row, grid.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(row);
}
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}
目前不知道该去哪里,并在整个网络上搜索了答案。我真的很感激任何帮助