单击按钮时,DataGridTextColumn显示NaN

时间:2018-10-24 04:58:00

标签: c# wpf wpfdatagrid

我有一个DataGrid,它显示每行不同的油名称,其使用百分比,滴计数和添加/减去滴按钮的列表。

为简单起见,假设名称为Pennziol,Castrol和Valvoline。它们的初始百分比使用值(假定为float)和放置计数(int)为0。如果单击一行的“ +”按钮,则放置计数将增加。如果随后将Pennziol加1,则其百分比使用率将变为100%,而其他百分比为0%。如果再单击两次嘉实多,其百分比将为66.6%,而Pennziol将减少至33.3%,而缬氨酸仍为0%。

我的代码假设每次单击按钮时都会执行此更新数据的操作,但是它将所有 Formula % 列单元格中的值作为 {{1} }

MainWindow.xaml.cs

NaN

Oil.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();
    }

OilList.cs

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

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

MainWindow.xaml

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

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

更新

<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> Oil继承了OilList,效果相同

INotifyPropertyChanged

2 个答案:

答案 0 :(得分:0)

我发现您的PropertyChanged中没有触发UsagePercentage事件。我认为您的财产的价值将适当更改,但是DataGrid不会更新。因此它保持其初始值似乎为NaN

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

答案 1 :(得分:0)

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

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

还要检查数据类型是否为所涉及的变量/属性的浮点型/两倍。