DataGrid Column's Element Style in CodeBehind has no effect
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
add a comment |
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
add a comment |
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
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
c# wpf wpfdatagrid
asked Dec 28 '18 at 16:31
cyboashucyboashu
8,69421535
8,69421535
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
};
thanks! It worked.
– cyboashu
Dec 28 '18 at 18:36
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
};
thanks! It worked.
– cyboashu
Dec 28 '18 at 18:36
add a comment |
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
};
thanks! It worked.
– cyboashu
Dec 28 '18 at 18:36
add a comment |
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
};
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
};
answered Dec 28 '18 at 18:17
teddy2teddy2
329115
329115
thanks! It worked.
– cyboashu
Dec 28 '18 at 18:36
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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