Changing a WPF Datagrid Row background color programmatically
I'm facing issues when trying to change a datagrid row in the code behind of a WPF app, my objectif is to change the row color when the row is selected and when a button "Valider" is clicked, my code is shown below.
I found some answers but none of them where usefull for my case.
private void Valider_Click(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = InventaireItemGrid.SelectedItem as DataGridRow;
dataGridRow.Background = Brushes.Green;
}
When I execute, I get a nullreferenceexception, the debugger point to the dataGridRow to be null (the row contains data though).
c# wpf datagrid
|
show 2 more comments
I'm facing issues when trying to change a datagrid row in the code behind of a WPF app, my objectif is to change the row color when the row is selected and when a button "Valider" is clicked, my code is shown below.
I found some answers but none of them where usefull for my case.
private void Valider_Click(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = InventaireItemGrid.SelectedItem as DataGridRow;
dataGridRow.Background = Brushes.Green;
}
When I execute, I get a nullreferenceexception, the debugger point to the dataGridRow to be null (the row contains data though).
c# wpf datagrid
have you checked if the particular row is not null?
– mahlatse
Nov 20 '18 at 11:49
Yes! @mahlatse, as I said, the row does contain data, but the variable dataGridRow is null.
– Saber CHETIOUI
Nov 20 '18 at 11:56
I don't know the why, but always try to make sure your data (DataGridRow
in your code) is notnull
. You can do something likeif (!(InventaireItemGrid.SelectedItem is DataGridRow dataGridRow) return;
– Gonzo345
Nov 20 '18 at 12:06
1
Isn't the selected item a property of your ViewModel? e.g if your datacontext is of type A, then the secleted item is of A.B where B is some property of A, what you should be looking at is filtering the sender object, cast it to the appropriate type type.
– mahlatse
Nov 20 '18 at 12:14
1
"as" returns null if the object has not the expected type. Set a breakpoint and check the type of InventaireItemGrid.SelectedItem at runtime. Knowing that type should hopefully tell you how to find the corresponding dataGridRow.
– Peter Huber
Nov 21 '18 at 14:44
|
show 2 more comments
I'm facing issues when trying to change a datagrid row in the code behind of a WPF app, my objectif is to change the row color when the row is selected and when a button "Valider" is clicked, my code is shown below.
I found some answers but none of them where usefull for my case.
private void Valider_Click(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = InventaireItemGrid.SelectedItem as DataGridRow;
dataGridRow.Background = Brushes.Green;
}
When I execute, I get a nullreferenceexception, the debugger point to the dataGridRow to be null (the row contains data though).
c# wpf datagrid
I'm facing issues when trying to change a datagrid row in the code behind of a WPF app, my objectif is to change the row color when the row is selected and when a button "Valider" is clicked, my code is shown below.
I found some answers but none of them where usefull for my case.
private void Valider_Click(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = InventaireItemGrid.SelectedItem as DataGridRow;
dataGridRow.Background = Brushes.Green;
}
When I execute, I get a nullreferenceexception, the debugger point to the dataGridRow to be null (the row contains data though).
c# wpf datagrid
c# wpf datagrid
asked Nov 20 '18 at 11:36
Saber CHETIOUISaber CHETIOUI
269
269
have you checked if the particular row is not null?
– mahlatse
Nov 20 '18 at 11:49
Yes! @mahlatse, as I said, the row does contain data, but the variable dataGridRow is null.
– Saber CHETIOUI
Nov 20 '18 at 11:56
I don't know the why, but always try to make sure your data (DataGridRow
in your code) is notnull
. You can do something likeif (!(InventaireItemGrid.SelectedItem is DataGridRow dataGridRow) return;
– Gonzo345
Nov 20 '18 at 12:06
1
Isn't the selected item a property of your ViewModel? e.g if your datacontext is of type A, then the secleted item is of A.B where B is some property of A, what you should be looking at is filtering the sender object, cast it to the appropriate type type.
– mahlatse
Nov 20 '18 at 12:14
1
"as" returns null if the object has not the expected type. Set a breakpoint and check the type of InventaireItemGrid.SelectedItem at runtime. Knowing that type should hopefully tell you how to find the corresponding dataGridRow.
– Peter Huber
Nov 21 '18 at 14:44
|
show 2 more comments
have you checked if the particular row is not null?
– mahlatse
Nov 20 '18 at 11:49
Yes! @mahlatse, as I said, the row does contain data, but the variable dataGridRow is null.
– Saber CHETIOUI
Nov 20 '18 at 11:56
I don't know the why, but always try to make sure your data (DataGridRow
in your code) is notnull
. You can do something likeif (!(InventaireItemGrid.SelectedItem is DataGridRow dataGridRow) return;
– Gonzo345
Nov 20 '18 at 12:06
1
Isn't the selected item a property of your ViewModel? e.g if your datacontext is of type A, then the secleted item is of A.B where B is some property of A, what you should be looking at is filtering the sender object, cast it to the appropriate type type.
– mahlatse
Nov 20 '18 at 12:14
1
"as" returns null if the object has not the expected type. Set a breakpoint and check the type of InventaireItemGrid.SelectedItem at runtime. Knowing that type should hopefully tell you how to find the corresponding dataGridRow.
– Peter Huber
Nov 21 '18 at 14:44
have you checked if the particular row is not null?
– mahlatse
Nov 20 '18 at 11:49
have you checked if the particular row is not null?
– mahlatse
Nov 20 '18 at 11:49
Yes! @mahlatse, as I said, the row does contain data, but the variable dataGridRow is null.
– Saber CHETIOUI
Nov 20 '18 at 11:56
Yes! @mahlatse, as I said, the row does contain data, but the variable dataGridRow is null.
– Saber CHETIOUI
Nov 20 '18 at 11:56
I don't know the why, but always try to make sure your data (
DataGridRow
in your code) is not null
. You can do something like if (!(InventaireItemGrid.SelectedItem is DataGridRow dataGridRow) return;
– Gonzo345
Nov 20 '18 at 12:06
I don't know the why, but always try to make sure your data (
DataGridRow
in your code) is not null
. You can do something like if (!(InventaireItemGrid.SelectedItem is DataGridRow dataGridRow) return;
– Gonzo345
Nov 20 '18 at 12:06
1
1
Isn't the selected item a property of your ViewModel? e.g if your datacontext is of type A, then the secleted item is of A.B where B is some property of A, what you should be looking at is filtering the sender object, cast it to the appropriate type type.
– mahlatse
Nov 20 '18 at 12:14
Isn't the selected item a property of your ViewModel? e.g if your datacontext is of type A, then the secleted item is of A.B where B is some property of A, what you should be looking at is filtering the sender object, cast it to the appropriate type type.
– mahlatse
Nov 20 '18 at 12:14
1
1
"as" returns null if the object has not the expected type. Set a breakpoint and check the type of InventaireItemGrid.SelectedItem at runtime. Knowing that type should hopefully tell you how to find the corresponding dataGridRow.
– Peter Huber
Nov 21 '18 at 14:44
"as" returns null if the object has not the expected type. Set a breakpoint and check the type of InventaireItemGrid.SelectedItem at runtime. Knowing that type should hopefully tell you how to find the corresponding dataGridRow.
– Peter Huber
Nov 21 '18 at 14:44
|
show 2 more comments
1 Answer
1
active
oldest
votes
The SelectedItem
property refers to the corresponding object in the Items
collection. You could use the ItemContainerGenerator
to get a reference to the DataGridRow
container:
private void Valider_Click(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = InventaireItemGrid.ItemContainerGenerator.ContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow;
if (dataGridRow != null)
dataGridRow.Background = Brushes.Green;
}
There are most probably better ways of doing whatever you are trying to do though, for example using data binding and triggers.
I already tried that while I was searching for answers, it did'nt work since the "ItemContainerGenerator" is obsolete in my framework and has been replaced by "GetContainerFromItem" method, therefore I tried this : DataGridRow dataGridRow = InventaireItemGrid.GetContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow; and I still get a nullreference.
– Saber CHETIOUI
Nov 20 '18 at 12:15
In "your" framework? What are you talking about? And what does the SelectedItem property return?
– mm8
Nov 20 '18 at 12:18
It's not a wpf object it's a control that I added, it's called Xceed Datagrid (it has a nice grouping, searching and sorting features) and it does'nt have the "ItemContainerGenerator" property anymore. I know I should change the header of my question then.
– Saber CHETIOUI
Nov 20 '18 at 12:38
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%2f53392174%2fchanging-a-wpf-datagrid-row-background-color-programmatically%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 SelectedItem
property refers to the corresponding object in the Items
collection. You could use the ItemContainerGenerator
to get a reference to the DataGridRow
container:
private void Valider_Click(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = InventaireItemGrid.ItemContainerGenerator.ContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow;
if (dataGridRow != null)
dataGridRow.Background = Brushes.Green;
}
There are most probably better ways of doing whatever you are trying to do though, for example using data binding and triggers.
I already tried that while I was searching for answers, it did'nt work since the "ItemContainerGenerator" is obsolete in my framework and has been replaced by "GetContainerFromItem" method, therefore I tried this : DataGridRow dataGridRow = InventaireItemGrid.GetContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow; and I still get a nullreference.
– Saber CHETIOUI
Nov 20 '18 at 12:15
In "your" framework? What are you talking about? And what does the SelectedItem property return?
– mm8
Nov 20 '18 at 12:18
It's not a wpf object it's a control that I added, it's called Xceed Datagrid (it has a nice grouping, searching and sorting features) and it does'nt have the "ItemContainerGenerator" property anymore. I know I should change the header of my question then.
– Saber CHETIOUI
Nov 20 '18 at 12:38
add a comment |
The SelectedItem
property refers to the corresponding object in the Items
collection. You could use the ItemContainerGenerator
to get a reference to the DataGridRow
container:
private void Valider_Click(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = InventaireItemGrid.ItemContainerGenerator.ContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow;
if (dataGridRow != null)
dataGridRow.Background = Brushes.Green;
}
There are most probably better ways of doing whatever you are trying to do though, for example using data binding and triggers.
I already tried that while I was searching for answers, it did'nt work since the "ItemContainerGenerator" is obsolete in my framework and has been replaced by "GetContainerFromItem" method, therefore I tried this : DataGridRow dataGridRow = InventaireItemGrid.GetContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow; and I still get a nullreference.
– Saber CHETIOUI
Nov 20 '18 at 12:15
In "your" framework? What are you talking about? And what does the SelectedItem property return?
– mm8
Nov 20 '18 at 12:18
It's not a wpf object it's a control that I added, it's called Xceed Datagrid (it has a nice grouping, searching and sorting features) and it does'nt have the "ItemContainerGenerator" property anymore. I know I should change the header of my question then.
– Saber CHETIOUI
Nov 20 '18 at 12:38
add a comment |
The SelectedItem
property refers to the corresponding object in the Items
collection. You could use the ItemContainerGenerator
to get a reference to the DataGridRow
container:
private void Valider_Click(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = InventaireItemGrid.ItemContainerGenerator.ContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow;
if (dataGridRow != null)
dataGridRow.Background = Brushes.Green;
}
There are most probably better ways of doing whatever you are trying to do though, for example using data binding and triggers.
The SelectedItem
property refers to the corresponding object in the Items
collection. You could use the ItemContainerGenerator
to get a reference to the DataGridRow
container:
private void Valider_Click(object sender, RoutedEventArgs e)
{
DataGridRow dataGridRow = InventaireItemGrid.ItemContainerGenerator.ContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow;
if (dataGridRow != null)
dataGridRow.Background = Brushes.Green;
}
There are most probably better ways of doing whatever you are trying to do though, for example using data binding and triggers.
answered Nov 20 '18 at 12:08
mm8mm8
85k81932
85k81932
I already tried that while I was searching for answers, it did'nt work since the "ItemContainerGenerator" is obsolete in my framework and has been replaced by "GetContainerFromItem" method, therefore I tried this : DataGridRow dataGridRow = InventaireItemGrid.GetContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow; and I still get a nullreference.
– Saber CHETIOUI
Nov 20 '18 at 12:15
In "your" framework? What are you talking about? And what does the SelectedItem property return?
– mm8
Nov 20 '18 at 12:18
It's not a wpf object it's a control that I added, it's called Xceed Datagrid (it has a nice grouping, searching and sorting features) and it does'nt have the "ItemContainerGenerator" property anymore. I know I should change the header of my question then.
– Saber CHETIOUI
Nov 20 '18 at 12:38
add a comment |
I already tried that while I was searching for answers, it did'nt work since the "ItemContainerGenerator" is obsolete in my framework and has been replaced by "GetContainerFromItem" method, therefore I tried this : DataGridRow dataGridRow = InventaireItemGrid.GetContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow; and I still get a nullreference.
– Saber CHETIOUI
Nov 20 '18 at 12:15
In "your" framework? What are you talking about? And what does the SelectedItem property return?
– mm8
Nov 20 '18 at 12:18
It's not a wpf object it's a control that I added, it's called Xceed Datagrid (it has a nice grouping, searching and sorting features) and it does'nt have the "ItemContainerGenerator" property anymore. I know I should change the header of my question then.
– Saber CHETIOUI
Nov 20 '18 at 12:38
I already tried that while I was searching for answers, it did'nt work since the "ItemContainerGenerator" is obsolete in my framework and has been replaced by "GetContainerFromItem" method, therefore I tried this : DataGridRow dataGridRow = InventaireItemGrid.GetContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow; and I still get a nullreference.
– Saber CHETIOUI
Nov 20 '18 at 12:15
I already tried that while I was searching for answers, it did'nt work since the "ItemContainerGenerator" is obsolete in my framework and has been replaced by "GetContainerFromItem" method, therefore I tried this : DataGridRow dataGridRow = InventaireItemGrid.GetContainerFromItem(InventaireItemGrid.SelectedItem) as DataGridRow; and I still get a nullreference.
– Saber CHETIOUI
Nov 20 '18 at 12:15
In "your" framework? What are you talking about? And what does the SelectedItem property return?
– mm8
Nov 20 '18 at 12:18
In "your" framework? What are you talking about? And what does the SelectedItem property return?
– mm8
Nov 20 '18 at 12:18
It's not a wpf object it's a control that I added, it's called Xceed Datagrid (it has a nice grouping, searching and sorting features) and it does'nt have the "ItemContainerGenerator" property anymore. I know I should change the header of my question then.
– Saber CHETIOUI
Nov 20 '18 at 12:38
It's not a wpf object it's a control that I added, it's called Xceed Datagrid (it has a nice grouping, searching and sorting features) and it does'nt have the "ItemContainerGenerator" property anymore. I know I should change the header of my question then.
– Saber CHETIOUI
Nov 20 '18 at 12:38
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%2f53392174%2fchanging-a-wpf-datagrid-row-background-color-programmatically%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
have you checked if the particular row is not null?
– mahlatse
Nov 20 '18 at 11:49
Yes! @mahlatse, as I said, the row does contain data, but the variable dataGridRow is null.
– Saber CHETIOUI
Nov 20 '18 at 11:56
I don't know the why, but always try to make sure your data (
DataGridRow
in your code) is notnull
. You can do something likeif (!(InventaireItemGrid.SelectedItem is DataGridRow dataGridRow) return;
– Gonzo345
Nov 20 '18 at 12:06
1
Isn't the selected item a property of your ViewModel? e.g if your datacontext is of type A, then the secleted item is of A.B where B is some property of A, what you should be looking at is filtering the sender object, cast it to the appropriate type type.
– mahlatse
Nov 20 '18 at 12:14
1
"as" returns null if the object has not the expected type. Set a breakpoint and check the type of InventaireItemGrid.SelectedItem at runtime. Knowing that type should hopefully tell you how to find the corresponding dataGridRow.
– Peter Huber
Nov 21 '18 at 14:44