WPF datagrid and rows coloring
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
add a comment |
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
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
add a comment |
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
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
c# wpf datagrid
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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>
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
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%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
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>
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
add a comment |
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>
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
add a comment |
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>
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>
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
add a comment |
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
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%2f53297155%2fwpf-datagrid-and-rows-coloring%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
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