I have a very strange problem and do not know the reason causes that.
I have a datagrid in my program which has a template column and some other regular columns. I used the template column because according to the data it contains I want it to change its backcolor. So I used a button control in my template column and added
a loaded event to this button you can see the datagrid code. I implemented the loaded event in my code, I am pasting the loaded event source code, too. Now the problem: When my datagrid first loads everthing is ok the color is changed according to the condition
in the event code and it works fine. The problem arises when I begin to scroll on my datagrid. The first picture below is the picture of my datagrid's first load, everthing is fine. The second picture is the picture when I scroll on my datagrid to the bottom
and come back to the top. Why after some scrolling the template column changes its color randomly I can not understand...
The remaining of the thread through this link, please tell me if this is a bug or not and also a workaround may be...
Hello, this is by design. You need to handle the DataGrid's UnloadingRow event and remove the backgrounds of the Buttons. But I think a better solution is to use a value converter. Something like this:
Public Class MyConverter
Implements IValueConverter
Public Function Convert(ByVal value
As Object,
ByVal targetType
As Type, ByVal parameter
As Object,
ByVal culture
As CultureInfo) As
Object Implements IValueConverter.Convert
'Add any logic here
Dim d
As Data = CType(value, Data)
If d.Name.StartsWith("a")
Then
Return
New SolidColorBrush(Colors.Red)
Else
Return
New SolidColorBrush(Colors.Blue)
End If
End Function
Public
Function ConvertBack(ByVal value
As Object,
ByVal targetType
As Type, ByVal parameter
As Object,
ByVal culture
As CultureInfo) As
Object Implements IValueConverter.ConvertBack
Return Nothing
End
Function
End
Class
To use this converter, you add it to the UserControl's Resources and bound the Button's Background to the data object itself:
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
Hi Yi-Lun thanks for your answer I added the class to my project and modified the color conditions also made the changes in the xaml file added the code you wrote but I have an error.
"The type 'local:MyConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. D:\Documents and Settings\ar002314\My Documents\Visual Studio 2008\Projects\SilverlightApplications\LOCALSilverlightHedefGosterge\SilverlightHedefGosterge\Page.xaml"
How will I add this reference? I really do not know to much about this can you explain me? I added the following line:
xmlns:local="clr-namespace:MyConverter;assembly=SilverlightHedefGosterge" and added the SilverlightHedefGosterge.dll to the references.
Hi
I faced the same problem it seems that first the grid loads all the rows and then unloads the rows that does not fall under the browser height. And after that during loading and unloading the grid miscalculates the row and the corresponding data.
The idea is to make block the silverlight from unloading the Rows that are not within the browser. Well the simplest way to do that is put the grid in a scrollviewer control and set the Width="Auto" Height="Auto".
Hope this will resolve your problem. Here is the sample code: -
Use databinding to achieve what you want. Bind the background of the DataGrid Cell to the Data, use a IValueConverter.
You can use =”{Binding}” to bind without specify any particular field. Some times when you are trying to use a Converter to return a calculated value based on more than one field in the record you can use this syntax to specify binding, so the value passed
to the IValueConverter will be the whole record of data which contains every fields.
In your case:
public class YourValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
YourDataObject data = value as YourDataObject;
if ((data.Desired - data.Actual) < data.Tolerance || (data.Desired - data.Actual) > data.Tolerance)
{
return new SolidColorBrush(Colors.Red);
}
}
...
}
Sally Xu
Software Engineer
Aprimo, Inc
Please remember to mark the replies as answers if they answered your question
You can use =”{Binding}” to bind without specify any particular field. Some times when you are trying to use a Converter to return a calculated value based on more than one field in the record you can use this syntax to specify binding, so the value passed
to the IValueConverter will be the whole record of data which contains every fields.
Sorry, I'm super green with these concepts, but I'm fairly certain this is exactly what I want to do [change the background color of an entire row based on the content of one column]. Could you please give a xaml example of what you mean by the above paragraph?
For example, below I want to change the color of the background based on a given timestamp value... where would I put the ={Binding} to do this?
If you need to change the background color of the DataGrid cell you have to use TemplateColumn and define your cell template. Because DataGridTextColumn does not have Background property that you can modify. DataGridTextColumn has Foreground property. The
following XAML set the binding to the Foreground property. But can change the DataGridTextColumn to a TemplateColumn and set the binding to the Background property in the root control (say a grid) in your CellTemplate:
PrisonBreak
Member
31 Points
61 Posts
Datagrid template column load event, strange behavior. Might be a bug
Sep 17, 2008 10:19 AM | LINK
Hi SL masters,
I have a very strange problem and do not know the reason causes that.
I have a datagrid in my program which has a template column and some other regular columns. I used the template column because according to the data it contains I want it to change its backcolor. So I used a button control in my template column and added a loaded event to this button you can see the datagrid code. I implemented the loaded event in my code, I am pasting the loaded event source code, too. Now the problem: When my datagrid first loads everthing is ok the color is changed according to the condition in the event code and it works fine. The problem arises when I begin to scroll on my datagrid. The first picture below is the picture of my datagrid's first load, everthing is fine. The second picture is the picture when I scroll on my datagrid to the bottom and come back to the top. Why after some scrolling the template column changes its color randomly I can not understand...
The remaining of the thread through this link, please tell me if this is a bug or not and also a workaround may be...
http://silverlight.net/forums/t/27465.aspx
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: Datagrid template column load event, strange behavior. Might be a bug
Sep 19, 2008 08:15 AM | LINK
Hello, this is by design. You need to handle the DataGrid's UnloadingRow event and remove the backgrounds of the Buttons. But I think a better solution is to use a value converter. Something like this:
Public Class MyConverter Implements IValueConverter Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert 'Add any logic here Dim d As Data = CType(value, Data) If d.Name.StartsWith("a") Then Return New SolidColorBrush(Colors.Red) Else Return New SolidColorBrush(Colors.Blue) End If End Function Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack Return Nothing End FunctionEnd
ClassTo use this converter, you add it to the UserControl's Resources and bound the Button's Background to the data object itself:
<UserControl.Resources> <local:MyConverter x:Key="mc"/> </UserControl.Resources> <Button Content="{Binding Name}" Background="{Binding Converter={StaticResource mc}}"/>PrisonBreak
Member
31 Points
61 Posts
Re: Re: Datagrid template column load event, strange behavior. Might be a bug
Sep 19, 2008 01:00 PM | LINK
Hi Yi-Lun thanks for your answer I added the class to my project and modified the color conditions also made the changes in the xaml file added the code you wrote but I have an error.
"The type 'local:MyConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. D:\Documents and Settings\ar002314\My Documents\Visual Studio 2008\Projects\SilverlightApplications\LOCALSilverlightHedefGosterge\SilverlightHedefGosterge\Page.xaml"
How will I add this reference? I really do not know to much about this can you explain me? I added the following line:
xmlns:local="clr-namespace:MyConverter;assembly=SilverlightHedefGosterge" and added the SilverlightHedefGosterge.dll to the references.
Where am I making the mistake?
sladapter
All-Star
43607 Points
7907 Posts
Re: Re: Datagrid template column load event, strange behavior. Might be a bug
Sep 19, 2008 02:19 PM | LINK
If you want to add the Converter definition in your UserControl.Resources section. Add the namespace prefix definition in the UserControl Tag:
<UserControl ...
xmlns:local="clr-namespace:TheNameSpaceOfYourConverter;assembly=TheAssmeblyNameOfYourConverter"
>
<UserControl.Resources>
<local:YourConverter x:Key="mc" />
</UserControl.Resources>
Software Engineer
Aprimo, Inc
Please remember to mark the replies as answers if they answered your question
Aditya Mahara
Member
2 Points
1 Post
Re: Datagrid template column load event, strange behavior. Might be a bug
Sep 28, 2008 09:12 PM | LINK
Hi
<ScrollViewer Width="Auto" Height="Auto" VerticalScrollBarVisibility="Visible" Grid.Row="0" Grid.Column="0" >I faced the same problem it seems that first the grid loads all the rows and then unloads the rows that does not fall under the browser height. And after that during loading and unloading the grid miscalculates the row and the corresponding data.
The idea is to make block the silverlight from unloading the Rows that are not within the browser. Well the simplest way to do that is put the grid in a scrollviewer control and set the Width="Auto" Height="Auto".
Hope this will resolve your problem. Here is the sample code: -
<
my:DataGrid /></
ScrollViewer.Content> </ScrollViewer>Regards
Aditya
mikep02
Member
6 Points
3 Posts
Re: Re: Datagrid template column load event, strange behavior. Might be a bug
Apr 24, 2009 02:34 AM | LINK
Hi,
Need help. I am new in silverlight.
I have these columns: Tolerance, Desired, Acual, and Variance.
I want the Variance column/cell background set to red if the difference of Desired and Actual column is not within the range set in Tolerance column.
e.g. if ((Desired - Actual) < Tolerance || (Desired - Actual) > Tolerance){
// set cell background color to red
}
How can I implement this using IValueConverter?
How can I do this both in grid's edit mode and loading event.
Thanks.
sladapter
All-Star
43607 Points
7907 Posts
Re: Re: Datagrid template column load event, strange behavior. Might be a bug
Apr 24, 2009 09:37 PM | LINK
mikep02,
Use databinding to achieve what you want. Bind the background of the DataGrid Cell to the Data, use a IValueConverter.
You can use =”{Binding}” to bind without specify any particular field. Some times when you are trying to use a Converter to return a calculated value based on more than one field in the record you can use this syntax to specify binding, so the value passed to the IValueConverter will be the whole record of data which contains every fields.
In your case:
public class YourValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
YourDataObject data = value as YourDataObject;
if ((data.Desired - data.Actual) < data.Tolerance || (data.Desired - data.Actual) > data.Tolerance)
{
return new SolidColorBrush(Colors.Red);
}
}
...
}
Software Engineer
Aprimo, Inc
Please remember to mark the replies as answers if they answered your question
mikep02
Member
6 Points
3 Posts
Re: Re: Re: Datagrid template column load event, strange behavior. Might be a bug
Apr 25, 2009 04:18 AM | LINK
It works!
thanks a lot [<:o)]
ssawchenko
Member
438 Points
214 Posts
Re: Re: Datagrid template column load event, strange behavior. Might be a bug
Sep 28, 2009 05:44 PM | LINK
Sorry, I'm super green with these concepts, but I'm fairly certain this is exactly what I want to do [change the background color of an entire row based on the content of one column]. Could you please give a xaml example of what you mean by the above paragraph?
For example, below I want to change the color of the background based on a given timestamp value... where would I put the ={Binding} to do this?
<Ctrls:DataGrid
x:Name="__TabDataGrid"
Grid.Row="1"
AutoGenerateColumns="False">
<Ctrls:DataGrid.Columns>
<Ctrls:DataGridTextColumn Header="Timestamp" Binding="{Binding TimeStamp}" />
<Ctrls:DataGridTextColumn Header="TL1" Binding="{Binding TL1Value}" />
<Ctrls:DataGridTextColumn Header="TL2" Binding="{Binding TL2Value}" />
</Ctrls:DataGrid.Columns>
</Ctrls:DataGrid>
sladapter
All-Star
43607 Points
7907 Posts
Re: Re: Datagrid template column load event, strange behavior. Might be a bug
Sep 29, 2009 09:05 AM | LINK
If you need to change the background color of the DataGrid cell you have to use TemplateColumn and define your cell template. Because DataGridTextColumn does not have Background property that you can modify. DataGridTextColumn has Foreground property. The following XAML set the binding to the Foreground property. But can change the DataGridTextColumn to a TemplateColumn and set the binding to the Background property in the root control (say a grid) in your CellTemplate:
<Ctrls:DataGrid.Columns>
<Ctrls:DataGridTextColumn Header="Timestamp" Binding="{Binding TimeStamp}" Foreground="{Binding TimeStamp, Converter={StaticResource YourColorConverter}}" />
<Ctrls:DataGridTextColumn Header="TL1" Binding="{Binding TL1Value}" />
<Ctrls:DataGridTextColumn Header="TL2" Binding="{Binding TL2Value}" />
</Ctrls:DataGrid.Columns>
Software Engineer
Aprimo, Inc
Please remember to mark the replies as answers if they answered your question