WPF datagrid and rows coloring












2















I'm trying to perform something that would be simple in WinForms, but turned to be close to impossible with WPF. I need to color rows (very long, a lot of values) using two shades (one for odd rows, one for even rows - aka AlternatingRowBackground). It seems that - if I set any brush to AlternatingRowBackground, whatever I'm trying to set over that (i.e. bound some value in my data table to make some rows highlighted with red background) - manipulating RowStyle or trying to force row background inside LoadedRow event function - all of it is ignored. If I will remove AlternatingRowBackground brush, RowStyle starts working, but I have no alternating rows shades.



So I tried to use MultiDataTrigger to (multi)bind this value that suppose to highlight and something that would show me row index - to manually create regular / alternating colors. I need that because DataGrid is filtered. I cannot use some fake value in data table to determine current row index. In other words - DataGrid row index is not same as data table row index. I tried to bind DataGridRow itself. But row, during conversion, has no index within datagrid (always -1).



Is there any way to have both AlternatingRowColor set and some rows highlighted with different background same time?










share|improve this question























  • May second answer in >THIS< question will be helpful. You can try with RowStyle instead of CellStyle.

    – Wojtman
    Nov 14 '18 at 9:52













  • It would be helpfully to see your XAML. Also please take a minute and read MCVE

    – Rekshino
    Nov 14 '18 at 9:55











  • Thanks for the tips, but - Wojtman - I think You meant using CellStyle instead RowStyle. Rekshino : I'm not doing it in XAML. for many reasons, I need to do that in code (not just one datagrid, an array of datagrids, etc.). To me - it's kind of a unclear where to add CellStyle. I did that in an event function that is serving AutoGenerating columns. However - in here - I still have no access to current RowIndex...

    – Vojtek
    Nov 14 '18 at 10:16


















2















I'm trying to perform something that would be simple in WinForms, but turned to be close to impossible with WPF. I need to color rows (very long, a lot of values) using two shades (one for odd rows, one for even rows - aka AlternatingRowBackground). It seems that - if I set any brush to AlternatingRowBackground, whatever I'm trying to set over that (i.e. bound some value in my data table to make some rows highlighted with red background) - manipulating RowStyle or trying to force row background inside LoadedRow event function - all of it is ignored. If I will remove AlternatingRowBackground brush, RowStyle starts working, but I have no alternating rows shades.



So I tried to use MultiDataTrigger to (multi)bind this value that suppose to highlight and something that would show me row index - to manually create regular / alternating colors. I need that because DataGrid is filtered. I cannot use some fake value in data table to determine current row index. In other words - DataGrid row index is not same as data table row index. I tried to bind DataGridRow itself. But row, during conversion, has no index within datagrid (always -1).



Is there any way to have both AlternatingRowColor set and some rows highlighted with different background same time?










share|improve this question























  • May second answer in >THIS< question will be helpful. You can try with RowStyle instead of CellStyle.

    – Wojtman
    Nov 14 '18 at 9:52













  • It would be helpfully to see your XAML. Also please take a minute and read MCVE

    – Rekshino
    Nov 14 '18 at 9:55











  • Thanks for the tips, but - Wojtman - I think You meant using CellStyle instead RowStyle. Rekshino : I'm not doing it in XAML. for many reasons, I need to do that in code (not just one datagrid, an array of datagrids, etc.). To me - it's kind of a unclear where to add CellStyle. I did that in an event function that is serving AutoGenerating columns. However - in here - I still have no access to current RowIndex...

    – Vojtek
    Nov 14 '18 at 10:16
















2












2








2








I'm trying to perform something that would be simple in WinForms, but turned to be close to impossible with WPF. I need to color rows (very long, a lot of values) using two shades (one for odd rows, one for even rows - aka AlternatingRowBackground). It seems that - if I set any brush to AlternatingRowBackground, whatever I'm trying to set over that (i.e. bound some value in my data table to make some rows highlighted with red background) - manipulating RowStyle or trying to force row background inside LoadedRow event function - all of it is ignored. If I will remove AlternatingRowBackground brush, RowStyle starts working, but I have no alternating rows shades.



So I tried to use MultiDataTrigger to (multi)bind this value that suppose to highlight and something that would show me row index - to manually create regular / alternating colors. I need that because DataGrid is filtered. I cannot use some fake value in data table to determine current row index. In other words - DataGrid row index is not same as data table row index. I tried to bind DataGridRow itself. But row, during conversion, has no index within datagrid (always -1).



Is there any way to have both AlternatingRowColor set and some rows highlighted with different background same time?










share|improve this question














I'm trying to perform something that would be simple in WinForms, but turned to be close to impossible with WPF. I need to color rows (very long, a lot of values) using two shades (one for odd rows, one for even rows - aka AlternatingRowBackground). It seems that - if I set any brush to AlternatingRowBackground, whatever I'm trying to set over that (i.e. bound some value in my data table to make some rows highlighted with red background) - manipulating RowStyle or trying to force row background inside LoadedRow event function - all of it is ignored. If I will remove AlternatingRowBackground brush, RowStyle starts working, but I have no alternating rows shades.



So I tried to use MultiDataTrigger to (multi)bind this value that suppose to highlight and something that would show me row index - to manually create regular / alternating colors. I need that because DataGrid is filtered. I cannot use some fake value in data table to determine current row index. In other words - DataGrid row index is not same as data table row index. I tried to bind DataGridRow itself. But row, during conversion, has no index within datagrid (always -1).



Is there any way to have both AlternatingRowColor set and some rows highlighted with different background same time?







c# wpf datagrid






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:45









VojtekVojtek

162




162













  • May second answer in >THIS< question will be helpful. You can try with RowStyle instead of CellStyle.

    – Wojtman
    Nov 14 '18 at 9:52













  • It would be helpfully to see your XAML. Also please take a minute and read MCVE

    – Rekshino
    Nov 14 '18 at 9:55











  • Thanks for the tips, but - Wojtman - I think You meant using CellStyle instead RowStyle. Rekshino : I'm not doing it in XAML. for many reasons, I need to do that in code (not just one datagrid, an array of datagrids, etc.). To me - it's kind of a unclear where to add CellStyle. I did that in an event function that is serving AutoGenerating columns. However - in here - I still have no access to current RowIndex...

    – Vojtek
    Nov 14 '18 at 10:16





















  • May second answer in >THIS< question will be helpful. You can try with RowStyle instead of CellStyle.

    – Wojtman
    Nov 14 '18 at 9:52













  • It would be helpfully to see your XAML. Also please take a minute and read MCVE

    – Rekshino
    Nov 14 '18 at 9:55











  • Thanks for the tips, but - Wojtman - I think You meant using CellStyle instead RowStyle. Rekshino : I'm not doing it in XAML. for many reasons, I need to do that in code (not just one datagrid, an array of datagrids, etc.). To me - it's kind of a unclear where to add CellStyle. I did that in an event function that is serving AutoGenerating columns. However - in here - I still have no access to current RowIndex...

    – Vojtek
    Nov 14 '18 at 10:16



















May second answer in >THIS< question will be helpful. You can try with RowStyle instead of CellStyle.

– Wojtman
Nov 14 '18 at 9:52







May second answer in >THIS< question will be helpful. You can try with RowStyle instead of CellStyle.

– Wojtman
Nov 14 '18 at 9:52















It would be helpfully to see your XAML. Also please take a minute and read MCVE

– Rekshino
Nov 14 '18 at 9:55





It would be helpfully to see your XAML. Also please take a minute and read MCVE

– Rekshino
Nov 14 '18 at 9:55













Thanks for the tips, but - Wojtman - I think You meant using CellStyle instead RowStyle. Rekshino : I'm not doing it in XAML. for many reasons, I need to do that in code (not just one datagrid, an array of datagrids, etc.). To me - it's kind of a unclear where to add CellStyle. I did that in an event function that is serving AutoGenerating columns. However - in here - I still have no access to current RowIndex...

– Vojtek
Nov 14 '18 at 10:16







Thanks for the tips, but - Wojtman - I think You meant using CellStyle instead RowStyle. Rekshino : I'm not doing it in XAML. for many reasons, I need to do that in code (not just one datagrid, an array of datagrids, etc.). To me - it's kind of a unclear where to add CellStyle. I did that in an event function that is serving AutoGenerating columns. However - in here - I still have no access to current RowIndex...

– Vojtek
Nov 14 '18 at 10:16














1 Answer
1






active

oldest

votes


















1














The problem is a precedence. Values set in attributes have higher precedence as set in styles. So if you set AlternatingRowBackground in a Style and not as element's attribute all will work as intended:



<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="Aquamarine"/>
</Style>
</DataGrid.Style>





share|improve this answer
























  • Excellent tip. Thank You. This is one of those things in WPF I couldn't find anywhere. Priorities of things.

    – Vojtek
    Nov 14 '18 at 10:25











  • You are welcome! :)

    – Rekshino
    Nov 14 '18 at 10:27











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%2f53297155%2fwpf-datagrid-and-rows-coloring%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














The problem is a precedence. Values set in attributes have higher precedence as set in styles. So if you set AlternatingRowBackground in a Style and not as element's attribute all will work as intended:



<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="Aquamarine"/>
</Style>
</DataGrid.Style>





share|improve this answer
























  • Excellent tip. Thank You. This is one of those things in WPF I couldn't find anywhere. Priorities of things.

    – Vojtek
    Nov 14 '18 at 10:25











  • You are welcome! :)

    – Rekshino
    Nov 14 '18 at 10:27
















1














The problem is a precedence. Values set in attributes have higher precedence as set in styles. So if you set AlternatingRowBackground in a Style and not as element's attribute all will work as intended:



<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="Aquamarine"/>
</Style>
</DataGrid.Style>





share|improve this answer
























  • Excellent tip. Thank You. This is one of those things in WPF I couldn't find anywhere. Priorities of things.

    – Vojtek
    Nov 14 '18 at 10:25











  • You are welcome! :)

    – Rekshino
    Nov 14 '18 at 10:27














1












1








1







The problem is a precedence. Values set in attributes have higher precedence as set in styles. So if you set AlternatingRowBackground in a Style and not as element's attribute all will work as intended:



<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="Aquamarine"/>
</Style>
</DataGrid.Style>





share|improve this answer













The problem is a precedence. Values set in attributes have higher precedence as set in styles. So if you set AlternatingRowBackground in a Style and not as element's attribute all will work as intended:



<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="Aquamarine"/>
</Style>
</DataGrid.Style>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 10:19









RekshinoRekshino

2,9391729




2,9391729













  • Excellent tip. Thank You. This is one of those things in WPF I couldn't find anywhere. Priorities of things.

    – Vojtek
    Nov 14 '18 at 10:25











  • You are welcome! :)

    – Rekshino
    Nov 14 '18 at 10:27



















  • Excellent tip. Thank You. This is one of those things in WPF I couldn't find anywhere. Priorities of things.

    – Vojtek
    Nov 14 '18 at 10:25











  • You are welcome! :)

    – Rekshino
    Nov 14 '18 at 10:27

















Excellent tip. Thank You. This is one of those things in WPF I couldn't find anywhere. Priorities of things.

– Vojtek
Nov 14 '18 at 10:25





Excellent tip. Thank You. This is one of those things in WPF I couldn't find anywhere. Priorities of things.

– Vojtek
Nov 14 '18 at 10:25













You are welcome! :)

– Rekshino
Nov 14 '18 at 10:27





You are welcome! :)

– Rekshino
Nov 14 '18 at 10:27




















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%2f53297155%2fwpf-datagrid-and-rows-coloring%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

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."