1

我对 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;
    }

目前不知道该去哪里,并在整个网络上搜索了答案。我真的很感激任何帮助

4

1 回答 1

1

使用价值转换器,类似于:

public class ZeroToBlank: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is int && (int)value == 0)
            return string.Empty;

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (string.IsNullOrEmpty(value))
            return 0;

        return value;
    } 
}

您需要在 XAML 中添加对命名空间的引用:

xmlns:converters="YourNamespace.Converters"

然后添加为资源:

<UserControl.Resources>
    <converters:ZeroToBlankConverter x:Key="ZeroToBlankConverter" />
</UserControl.Resources>

最后在绑定中:

Binding="{Binding TranDeposit, Converter={StaticResource ZeroToBlankConverter}}"
于 2015-03-22T11:49:30.913 回答