WPF DataGridTextColumn - 使用' - '而不是'0'

时间:2011-12-13 19:15:10

标签: wpf wpfdatagrid ivalueconverter datagridtextcolumn

如何让DataGridTextColumn显示' - (连字符)而不是 0

以下是数据网格当前的设置方式:

<Grid>
  <DataGrid x:Name="EmployeeHours"
            AutoGenerateColumns="False"
            ItemsSource="{Binding EmployeeHoursLastWeek}"
            Width="Auto">
    <DataGrid.Columns>
      <DataGridTextColumn Header="PerceptionistID"
                          Binding="{Binding PerceptionistID}"
                          Width="100" />
      <DataGridTextColumn Header="Week Of"
                          Binding="{Binding WeekOf, StringFormat={}{0:MM/dd/yyyy}}"
                          Width="75" />
      <DataGridTextColumn Header="Regular Hours"
                          Binding="{Binding WorkHours}"
                          Width="100" />
      <DataGridTextColumn Header="PTO Hours"
                          Binding="{Binding PTOHours}"
                          Width="100" />
      <DataGridTextColumn Header="Holiday Hours"
                          Binding="{Binding HolidayHours}"
                          Width="100" />
    </DataGrid.Columns>
  </DataGrid>
</Grid>

PTOHours和HolidayHours通常为0,如果0的任何单元格显示为“ - ”,则可以更容易地识别值不为0的单元格。

1 个答案:

答案 0 :(得分:4)

你可以使用值转换器

<Grid>
  <Grid.Resources>
    <local:HoursToMinusConverter x:Key="HoursToMinusConverter" />
  </Grid.Resources>

  <DataGrid x:Name="EmployeeHours"
            AutoGenerateColumns="False"
            ItemsSource="{Binding EmployeeHoursLastWeek}"
            Width="Auto">
    <DataGrid.Columns>
      <DataGridTextColumn Header="PerceptionistID"
                          Binding="{Binding PerceptionistID}"
                          Width="100" />
      <DataGridTextColumn Header="Week Of"
                          Binding="{Binding WeekOf, StringFormat={}{0:MM/dd/yyyy}}"
                          Width="75" />
      <DataGridTextColumn Header="Regular Hours"
                          Binding="{Binding WorkHours}"
                          Width="100" />
      <DataGridTextColumn Header="PTO Hours"
                          Binding="{Binding PTOHours, Converter={StaticResource HoursToMinusConverter}}"
                          Width="100" />
      <DataGridTextColumn Header="Holiday Hours"
                          Binding="{Binding HolidayHours, Converter={StaticResource HoursToMinusConverter}}"
                          Width="100" />
    </DataGrid.Columns>
  </DataGrid>
</Grid>

这是转换器

public class HoursToMinusConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    if (value is int) {
      return (int)value > 0 ? value.ToString() : "-";
    }
    return string.Empty;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
    if (value is string) {
      int intValue;
      if (!int.TryParse((string)value, out intValue)) {
        intValue = 0;
      }
      return intValue;
    }
    return 0;
  }
}

希望这会有所帮助