DataGrid Column's Element Style in CodeBehind has no effect












1















I am setting column's Element style in code behind using MultiConverter. Even though the converter is being accessed and there's no error at all, cell's background is not getting updated.



private void DgBinding(DataTable dt)
{
string prevCol = "";

foreach (DataColumn dc in dt.Columns)
{
if (dc.ColumnName.StartsWith("Delta"))
{
prevCol = dc.ColumnName;
continue;
}
DataGridTextColumn col = new DataGridTextColumn
{
Header = dc.ColumnName,
Binding = new Binding(dc.ColumnName)
};

this.dgTarget.Columns.Add(col);

if (!string.IsNullOrEmpty(prevCol) && prevCol.StartsWith("Delta"))
{
MultiBinding m = new MultiBinding {Converter = new TimeSeriesColorConverter()};

m.Bindings.Add(new Binding(dc.ColumnName));
m.Bindings.Add(new Binding(prevCol));

Style style = new Style();
style.TargetType = typeof(TextBlock);

Setter setter = new Setter
{
Property = BackgroundProperty,
Value = m
};

style.Setters.Add(setter);

col.ElementStyle = style;
}
prevCol = dc.ColumnName;
}
}


If I just use, col.CellStyle it works and the background's are getting updated, but with ElementStyle there's no effect at all. Any idea why?



I can't use XAML as the data is dynamic timeseries and # of Columns are unknown.










share|improve this question



























    1















    I am setting column's Element style in code behind using MultiConverter. Even though the converter is being accessed and there's no error at all, cell's background is not getting updated.



    private void DgBinding(DataTable dt)
    {
    string prevCol = "";

    foreach (DataColumn dc in dt.Columns)
    {
    if (dc.ColumnName.StartsWith("Delta"))
    {
    prevCol = dc.ColumnName;
    continue;
    }
    DataGridTextColumn col = new DataGridTextColumn
    {
    Header = dc.ColumnName,
    Binding = new Binding(dc.ColumnName)
    };

    this.dgTarget.Columns.Add(col);

    if (!string.IsNullOrEmpty(prevCol) && prevCol.StartsWith("Delta"))
    {
    MultiBinding m = new MultiBinding {Converter = new TimeSeriesColorConverter()};

    m.Bindings.Add(new Binding(dc.ColumnName));
    m.Bindings.Add(new Binding(prevCol));

    Style style = new Style();
    style.TargetType = typeof(TextBlock);

    Setter setter = new Setter
    {
    Property = BackgroundProperty,
    Value = m
    };

    style.Setters.Add(setter);

    col.ElementStyle = style;
    }
    prevCol = dc.ColumnName;
    }
    }


    If I just use, col.CellStyle it works and the background's are getting updated, but with ElementStyle there's no effect at all. Any idea why?



    I can't use XAML as the data is dynamic timeseries and # of Columns are unknown.










    share|improve this question

























      1












      1








      1








      I am setting column's Element style in code behind using MultiConverter. Even though the converter is being accessed and there's no error at all, cell's background is not getting updated.



      private void DgBinding(DataTable dt)
      {
      string prevCol = "";

      foreach (DataColumn dc in dt.Columns)
      {
      if (dc.ColumnName.StartsWith("Delta"))
      {
      prevCol = dc.ColumnName;
      continue;
      }
      DataGridTextColumn col = new DataGridTextColumn
      {
      Header = dc.ColumnName,
      Binding = new Binding(dc.ColumnName)
      };

      this.dgTarget.Columns.Add(col);

      if (!string.IsNullOrEmpty(prevCol) && prevCol.StartsWith("Delta"))
      {
      MultiBinding m = new MultiBinding {Converter = new TimeSeriesColorConverter()};

      m.Bindings.Add(new Binding(dc.ColumnName));
      m.Bindings.Add(new Binding(prevCol));

      Style style = new Style();
      style.TargetType = typeof(TextBlock);

      Setter setter = new Setter
      {
      Property = BackgroundProperty,
      Value = m
      };

      style.Setters.Add(setter);

      col.ElementStyle = style;
      }
      prevCol = dc.ColumnName;
      }
      }


      If I just use, col.CellStyle it works and the background's are getting updated, but with ElementStyle there's no effect at all. Any idea why?



      I can't use XAML as the data is dynamic timeseries and # of Columns are unknown.










      share|improve this question














      I am setting column's Element style in code behind using MultiConverter. Even though the converter is being accessed and there's no error at all, cell's background is not getting updated.



      private void DgBinding(DataTable dt)
      {
      string prevCol = "";

      foreach (DataColumn dc in dt.Columns)
      {
      if (dc.ColumnName.StartsWith("Delta"))
      {
      prevCol = dc.ColumnName;
      continue;
      }
      DataGridTextColumn col = new DataGridTextColumn
      {
      Header = dc.ColumnName,
      Binding = new Binding(dc.ColumnName)
      };

      this.dgTarget.Columns.Add(col);

      if (!string.IsNullOrEmpty(prevCol) && prevCol.StartsWith("Delta"))
      {
      MultiBinding m = new MultiBinding {Converter = new TimeSeriesColorConverter()};

      m.Bindings.Add(new Binding(dc.ColumnName));
      m.Bindings.Add(new Binding(prevCol));

      Style style = new Style();
      style.TargetType = typeof(TextBlock);

      Setter setter = new Setter
      {
      Property = BackgroundProperty,
      Value = m
      };

      style.Setters.Add(setter);

      col.ElementStyle = style;
      }
      prevCol = dc.ColumnName;
      }
      }


      If I just use, col.CellStyle it works and the background's are getting updated, but with ElementStyle there's no effect at all. Any idea why?



      I can't use XAML as the data is dynamic timeseries and # of Columns are unknown.







      c# wpf wpfdatagrid






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 16:31









      cyboashucyboashu

      8,69421535




      8,69421535
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You are using TargetType as TextBlock but when setting the property inside setter you are referring to DataGridCell's BackgroundProperty. When elemesntstyle looks for TextBlock changes, it finds nothing and no change occurs.



          As for CellStyle, Setter works because of the same reason.



          Change your code to this :



                   Setter setter = new Setter
          {
          Property = TextBlock.BackgroundProperty,
          Value = m
          };





          share|improve this answer
























          • thanks! It worked.

            – cyboashu
            Dec 28 '18 at 18:36











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961533%2fdatagrid-columns-element-style-in-codebehind-has-no-effect%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You are using TargetType as TextBlock but when setting the property inside setter you are referring to DataGridCell's BackgroundProperty. When elemesntstyle looks for TextBlock changes, it finds nothing and no change occurs.



          As for CellStyle, Setter works because of the same reason.



          Change your code to this :



                   Setter setter = new Setter
          {
          Property = TextBlock.BackgroundProperty,
          Value = m
          };





          share|improve this answer
























          • thanks! It worked.

            – cyboashu
            Dec 28 '18 at 18:36
















          1














          You are using TargetType as TextBlock but when setting the property inside setter you are referring to DataGridCell's BackgroundProperty. When elemesntstyle looks for TextBlock changes, it finds nothing and no change occurs.



          As for CellStyle, Setter works because of the same reason.



          Change your code to this :



                   Setter setter = new Setter
          {
          Property = TextBlock.BackgroundProperty,
          Value = m
          };





          share|improve this answer
























          • thanks! It worked.

            – cyboashu
            Dec 28 '18 at 18:36














          1












          1








          1







          You are using TargetType as TextBlock but when setting the property inside setter you are referring to DataGridCell's BackgroundProperty. When elemesntstyle looks for TextBlock changes, it finds nothing and no change occurs.



          As for CellStyle, Setter works because of the same reason.



          Change your code to this :



                   Setter setter = new Setter
          {
          Property = TextBlock.BackgroundProperty,
          Value = m
          };





          share|improve this answer













          You are using TargetType as TextBlock but when setting the property inside setter you are referring to DataGridCell's BackgroundProperty. When elemesntstyle looks for TextBlock changes, it finds nothing and no change occurs.



          As for CellStyle, Setter works because of the same reason.



          Change your code to this :



                   Setter setter = new Setter
          {
          Property = TextBlock.BackgroundProperty,
          Value = m
          };






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 18:17









          teddy2teddy2

          329115




          329115













          • thanks! It worked.

            – cyboashu
            Dec 28 '18 at 18:36



















          • thanks! It worked.

            – cyboashu
            Dec 28 '18 at 18:36

















          thanks! It worked.

          – cyboashu
          Dec 28 '18 at 18:36





          thanks! It worked.

          – cyboashu
          Dec 28 '18 at 18:36


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961533%2fdatagrid-columns-element-style-in-codebehind-has-no-effect%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas