如何根据单元格值更改wpf Databound网格单元格?

我对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事件,但是,当试图使用非常流行的DataGrid助手扩展“DataGridHelper”时,我在GetCell方法中得到了一个N​​ullReferenceException:

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

不知道该走到哪里,并在网上搜索了一个答案。 我非常感谢任何帮助


使用一个值转换器,如下所示:

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}}"
链接地址: http://www.djcxy.com/p/71121.html

上一篇: How can I change a wpf Databound grid cell based on the cell value?

下一篇: How to edit an unbound cell in a bound DataGrid?