WPF DataGrid基于变量的行颜色更改

时间:2015-07-28 06:25:27

标签: c# wpf datagrid wpf-controls

我使用数据触发器来更改DataGrid视图组件中行的颜色。 代码是:

class QuantityToColorConverter : IValueConverter
{
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (int)value <= 444 ?
                new SolidColorBrush(Colors.Red)
                : new SolidColorBrush(Colors.White);
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }
}

如何更换&#39; 444&#39;一个变量的值,该变量具有在button_click函数中计算网格单元格值的结果?

编辑:更清楚地说明我需要的内容:我想要更改其中一列中的值大于平均值的行的颜色。由于平均值是根据DataGrid数据计算的,我需要将其作为变量而不是444常量发送。

EDIT2:按钮代码:

private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var engine = new FileHelperEngine<ActionLog>();

        var result = engine.ReadFile("browse.csv");
        // result is now an array of ActionLog

        var actionsCnt = new int[22];

        int curAccessId =  result[1].AccessId;
        int AccessCount = 0;

        foreach (var record in result)
        {
            actionsCnt[record.ActionType]++;
            if (record.AccessId != curAccessId) { curAccessId = record.AccessId;  AccessCount++; }
        }

        quantityThreshold = AccessCount;

        List<act> myList = new List<act>();
        for (int i = 0; i < 22; i++)
            myList.Add(new act() { actionID = i, quantity = actionsCnt[i] });

        grid1.ItemsSource = myList;

        engine.WriteFile("FileOut.csv", result);
    }

quantityThreshold 是我想要使用的变量,而不是&#39; 444&#39;

3 个答案:

答案 0 :(得分:2)

将计算变量绑定到Converter ConverterParameter。请参阅Binding to Converter Parameter

上的此主题
 return (int)value <= (int)parameter?
                new SolidColorBrush(Colors.Red)
                : new SolidColorBrush(Colors.White);

答案 1 :(得分:1)

试试这个

   <?php
       $servername = "localhost";
       $username = "root";
       $password = "";
       $dbname = "pacra-daily";

       $sql="SELECT pacra_teams.title as 'teamTitle', og_users.display_name,                    og_users.id
            FROM og_users
            LEFT JOIN pacra_teams
            ON pacra_teams.id = og_users.team_id
            Where og_users.id = 106";
       $mysqli = new mysqli($servername , $username, $password, $dbname);

       /* check connection */
       if ($mysqli->connect_errno) {
           printf("Connect failed: %s\n", $mysqli->connect_error);
           exit();
       }


       /* Select queries return a resultset */
       if ($result = $mysqli->query($sql)) {
                printf("Select returned %d rows.\n", $result->num_rows);

                /* free result set */
                $result->close();
      }

      /* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
      if ($result = $mysqli->query($sql, MYSQLI_USE_RESULT)) {

      /* Note, that we can't execute any functions which interact with the
         server until result set was closed. All calls will return an
         'out of sync' error */
      if (!$mysqli->query("SET @a:='this will not work'")) {
          printf("Error: %s\n", $mysqli->error);
      }
      $result->close();
     }

     $mysqli->close();
 ?>

refer this link

答案 2 :(得分:0)

您可以将参数传递给转换器,就像这样

...
Binding="{Binding ValueToBind, Converter={SomeConverter},ConverterParameter=YourParameteres}"
...

然后在转换器中将其用作对象参数