如何根据单元格值更改 wpf Databound 网格单元格?
How can I change a wpf Databound grid cell based on the cell value?
我对 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;
}
此时不确定去哪里,并在网上搜索了答案。我真的很感激任何帮助
使用值转换器,类似于:
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}}"
我对 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;
}
此时不确定去哪里,并在网上搜索了答案。我真的很感激任何帮助
使用值转换器,类似于:
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}}"