Hi,
I have a need of supplying some formatting info for each row based on certain criteria. This formatting info is not main data so, it doesn't make sense to set the formatInfo object as DataContext.
My idea was/is to add appropriate formatInfo object to Resource of current row (effectively, adding a key-object to e.Row.Resources, say 'formatObject' as key, and object of formatInfo based on the criteria) in RowLoaded event handler.
I wanted to make use of the added Resource 'formatObject' in Row Template. But due to the resulted error it seems that the template is used before RowLoaded event gets opportunity to add the resource.
What is proper way to supply 'StaticResource' to row dynamically.?
________________________________________________________________________________________
Part II of the story:
Without setting binding to 'formatObject' in the template, is wrote code to add the formatInfo object to Resources. It failed with strange error.
Following is code in the event handler, so we are on same page if the description is not good enough.
The error is, 'Element is already the child of another element.' for the second row.
Interestingly, on line,
that is to say, that
I tried doing something similar, and it worked.
Please do let me know if anything is unclear.
Regards,
Kinjal
I have a need of supplying some formatting info for each row based on certain criteria. This formatting info is not main data so, it doesn't make sense to set the formatInfo object as DataContext.
My idea was/is to add appropriate formatInfo object to Resource of current row (effectively, adding a key-object to e.Row.Resources, say 'formatObject' as key, and object of formatInfo based on the criteria) in RowLoaded event handler.
I wanted to make use of the added Resource 'formatObject' in Row Template. But due to the resulted error it seems that the template is used before RowLoaded event gets opportunity to add the resource.
What is proper way to supply 'StaticResource' to row dynamically.?
________________________________________________________________________________________
Part II of the story:
Without setting binding to 'formatObject' in the template, is wrote code to add the formatInfo object to Resources. It failed with strange error.
Following is code in the event handler, so we are on same page if the description is not good enough.
if
(!(e.Row
is
GridViewHeaderRow))
{
if
(e.DataElement !=
null
)
{
if
(e.Row.Resources.Contains(
"formatObject"
))
e.Row.Resources[
"formatObject"
] = ((Result)e.DataElement).Pass ? formatInfoPass : formatInfoFail;
else
e.Row.Resources.Add(
"formatObject"
, ((Result)e.DataElement).Pass ? formatInfoPass : formatInfoFail);
}
}
The error is, 'Element is already the child of another element.' for the second row.
Interestingly, on line,
"
e.Row.Resources.Add(
"formatObject"
, ((Result)e.DataElement).Pass ? formatInfoPass : formatInfoFail);
"that is to say, that
e.Row.Resources.Contains(
"formatObject"
)
resulted false. (by the way, the Resources collection count was 0)I tried doing something similar, and it worked.
<
Button
Width
=
"100"
Height
=
"25"
x:Name
=
"b1"
>
<
Button.Resources
>
<
SolidColorBrush
x:Key
=
"CurrentBackground"
Color
=
"Green"
></
SolidColorBrush
>
</
Button.Resources
>
<
Button.Content
>
<
StackPanel
Background
=
"{StaticResource CurrentBackground}"
>
<
TextBlock
Text
=
"Button111"
></
TextBlock
>
</
StackPanel
>
</
Button.Content
>
</
Button
>
<
Button
Width
=
"100"
Height
=
"25"
x:Name
=
"b2"
>
<
Button.Resources
>
<
SolidColorBrush
x:Key
=
"CurrentBackground"
Color
=
"Orange"
></
SolidColorBrush
>
</
Button.Resources
>
<
Button.Content
>
<
StackPanel
Background
=
"{StaticResource CurrentBackground}"
>
<
TextBlock
Text
=
"Button2"
></
TextBlock
>
</
StackPanel
>
</
Button.Content
>
</
Button
>
Please do let me know if anything is unclear.
Regards,
Kinjal