单击按钮时 DataGridTextColumn 显示 NaN

DataGridTextColumn displays NaN when button is clicked

我有一个 DataGrid,显示不同油品名称的列表、它们的使用百分比、滴数和每行 add/subtract 滴按钮。

为简单起见,假设名称为 Pennziol、Castrol 和 Valvoline。它们的初始百分比使用值(假设为浮点数)和滴数(整数)为 0。如果单击某行的“+”按钮,则滴数将增加。如果 Pennziol 随后递增 1,则其使用百分比将变为 100%,而其他剩余为 0%。如果再点击两次 Castrol,它的百分比将是 66.6%,Pennziol 将减少到 33.3%,Valvoline 剩余 0%。

我的代码假设在每次单击按钮时更新数据,但它 returns 所有 Formula % 列中的值单元格为 NaN.

MainWindow.xaml.cs

private OilList mOilList = new OilList();

    public MainWindow()
    {
        InitializeComponent();

        // Add Oils to Oil Grid Table
        foreach (Oil oil in mOilList.Oils)
        {
            OilGridXaml.Items.Add(oil);        
        }          
    }


    private void Button_Click_Increment(object sender, RoutedEventArgs e)
    {        
        Oil oil = ((FrameworkElement)sender).DataContext as Oil;
        oil.AddDrop();

        mOilList.UpdateFormulaWeight();

        PerfumeGridXaml.Items.Refresh();
    }

Oil.cs

public void AddDrop()
    {
        mDropCount++;
        mOilAmount = mDropSize * mDropCount;
        NotifyPropertyChanged("DropCount");
    }

public float UsagePercentage
    {
        get { return mUsagePercentage; }
        set
        {
            if (mUsagePercentage != value)
            { 
                mUsagePercentage = value;
                NotifyPropertyChanged("UsagePercentage");
            }
        }
    }

OilList.cs

public void UpdateFormulaWeight()
    {
        foreach (Oil oil in mOils)
            mFormulaTotalWeight += oil.TotalWeight;

        foreach (Oil oil in mOils)
        {
            oil.UsagePercentage = (oil.TotalWeight / mFormulaTotalWeight) * 100f;
            NotifyPropertyChanged("UsagePercentage");
        }
    }

MainWindow.xaml

<DataGrid x:name="OilGridXaml" ... ItemsSource="{Binding}">...
<!-- USAGE PERCENTAGE -->
<DataGridTextColumn Header="Formula %" Foreground="LightGray" Binding="{Binding UsagePercentage, StringFormat={}{0:F2}%}" IsReadOnly="True" Width="64">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="#1e1e1e"/>
        </Style>
    </DataGridTextColumn.CellStyle>

    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextBlock.VerticalAlignment" Value="Center" />
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

<!-- DROP COUNT -->
<DataGridTextColumn x:Name="Cell_Drop_Count" Header="Drop Count" Foreground="LightGray" Binding="{Binding DropCount}" Width="*">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="#1e1e1e"/>
        </Style>
    </DataGridTextColumn.CellStyle>

    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextBlock.VerticalAlignment" Value="Center" />
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

更新

OilOilList 继承 INotifyPropertyChanged 效果相同

public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

我发现您的 UsagePercentage 中没有触发任何 PropertyChanged 事件。我认为您的 属性 的值会正确更改,但 DataGrid 不会更新。所以它保持初始值似乎是 NaN.

PerfumeGridXaml.Items.Refresh() 只会重新创建视图,不会刷新 ItemsSource 或其任何元素。

在您的代码中,如果 (oil.TotalWeight / mFormulaTotalWeight) 此除法的结果为 0,那么您将根据下面提到的代码行获得 NaN 值作为输出。

oil.UsagePercentage = (oil.TotalWeight / mFormulaTotalWeight) * 100f;

还要检查数据类型是否是涉及的Variables/Properties的floating/double。