This is a migrated thread and some comments may be shown as answers.

Databinding difficulty with color

3 Answers 171 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 14 Oct 2013, 02:48 PM
Hi,
I seem to be unable to get the databinding to work so that, for example, the background colour of a grid cell is set to the colour property of a business object. Please can someone take a look and show me the error of my ways?

ORM version 2013.2.702.1
RadControls for WPF version 2013.2.724.40

In summary, I am getting a list back from the database using a stored procedure mapped to a domain method. I have also added a partial class and a transient property to the result item in my ORM library. This transient property is a System.Windows.Media.Color and the values are stored in an XML file that are merged with the database result by my view model prior to the view being presented.

In my attempt to understand why the colour is not set, I am showing the ToString result in the cell so I know the colour value is present, however, the colour of the cell remains white.

Here's my ORM partial class:
using System;
using System.Linq;
using System.Windows.Media;
 
namespace xyz
{
    public partial class DBLISTitem
    {
        [Telerik.OpenAccess.Transient]
        private Color _patternColour;
        public Color PatternColour
        {
            get
            {
                return _patternColour;
            }
            set
            {
                _patternColour = value;
            }
        }
    }
}

Here's the XAML cut down for brevity:
<UserControl x:Class="..."
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="5"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Grid.Resources>
            <telerik:ColorToBrushConverter x:Key="ColorToBrushConverter" />
        </Grid.Resources>
 
        <telerik:RadGridView Name="ocyMapyGrid" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3"
                             HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="Auto" IsFilteringAllowed="False" CanUserSortColumns="True" CanUserSortGroups="True" 
                             ItemsSource="{Binding DBLIST, Mode=TwoWay}" AutoGenerateColumns="False" ShowGroupPanel="False"
                             ShowInsertRow="False"
                             RowHeight="23" RowIndicatorVisibility="Collapsed" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                             CanUserFreezeColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="True"
                             >
                <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding ColName}" Header="Col1" Width="*" />
                <telerik:GridViewDataColumn Header="Colour" Width="150">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=PatternColour}" Background="{Binding Path=PatternColour, Converter={StaticResource ColorToBrushConverter}}" Margin="0" />
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

I've attached a sample of the output view.

I've since worked on a very simple test project to try to reproduce the problem but I cannot. The test app works. I changed the cell template in the problem app to match the working test app as follows:
<telerik:GridViewDataColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Path=HatchPatternColour}" Margin="0">
            <TextBlock.Background>
                <SolidColorBrush Color="{Binding Path=HatchPatternColour}" />
            </TextBlock.Background>
        </TextBlock>
    </DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
This made no difference. The backgound color remains white.

Further to this, I've increased the trace level to high and get this output for the SolidColorBrush binding:
System.Windows.Data Warning: 54 : Created BindingExpression (hash=20902088) for Binding (hash=2322454)
System.Windows.Data Warning: 56 :   Path: 'HatchPatternColour'
System.Windows.Data Warning: 58 : BindingExpression (hash=20902088): Default mode resolved to OneWay
System.Windows.Data Warning: 59 : BindingExpression (hash=20902088): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 60 : BindingExpression (hash=20902088): Attach to System.Windows.Media.SolidColorBrush.Color (hash=53901065)
System.Windows.Data Warning: 62 : BindingExpression (hash=20902088): Use Framework mentor <null>
System.Windows.Data Warning: 65 : BindingExpression (hash=20902088): Resolving source 
System.Windows.Data Warning: 67 : BindingExpression (hash=20902088): Framework mentor not found
System.Windows.Data Warning: 63 : BindingExpression (hash=20902088): Resolve source deferred
System.Windows.Data Warning: 93 : BindingExpression (hash=20902088): Got InheritanceContextChanged event from SolidColorBrush (hash=53901065)
System.Windows.Data Warning: 65 : BindingExpression (hash=20902088): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=20902088): Found data context element: TextBlock (hash=15347538) (OK)
System.Windows.Data Warning: 69 : BindingExpression (hash=20902088): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=20902088): Resolving source 
System.Windows.Data Warning: 68 : BindingExpression (hash=20902088): Found data context element: TextBlock (hash=15347538) (OK)
System.Windows.Data Warning: 76 : BindingExpression (hash=20902088): Activate with root item TopLevelAccountCode (hash=28952916)
System.Windows.Data Warning: 106 : BindingExpression (hash=20902088):   At level 0 - for TopLevelAccountCode.HatchPatternColour found accessor ReflectPropertyDescriptor(HatchPatternColour)
System.Windows.Data Warning: 102 : BindingExpression (hash=20902088): Replace item at level 0 with TopLevelAccountCode (hash=28952916), using accessor ReflectPropertyDescriptor(HatchPatternColour)
System.Windows.Data Warning: 99 : BindingExpression (hash=20902088): GetValue at level 0 from TopLevelAccountCode (hash=28952916) using ReflectPropertyDescriptor(HatchPatternColour): Color (hash=1871599772)
System.Windows.Data Warning: 78 : BindingExpression (hash=20902088): TransferValue - got raw value Color (hash=1871599772)
System.Windows.Data Warning: 87 : BindingExpression (hash=20902088): TransferValue - using final value Color (hash=1871599772)
Doesn't this show that it has found the Color property?

Any suggestions please?

Thanks
Craig

3 Answers, 1 is accepted

Sort by
0
Craig
Top achievements
Rank 1
answered on 16 Oct 2013, 09:24 AM
Hi
No one spotted the obvious from the attached image? ;) 
Some how the colour was saved with the alpha channel value set to 0, i.e. transparent.
Something to watch out for in the future.
Craig
0
Nick
Telerik team
answered on 16 Oct 2013, 11:07 AM
Hi Craig,

Glad to hear you figured it out. Nevertheless, we strongly advise our customers to use StyleSelectors in cases like yours. You can see an example here.

Hope this makes sense! 

Regards,
Nik
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Craig
Top achievements
Rank 1
answered on 16 Oct 2013, 12:12 PM
Hi Nik,
Yes it does make sense. I had looked at the WPF demos in the same section.
For may case I just needed the converter with my conversion returning true or false.
I also know why the data had 0 in alpha channel; I had saved it that way unknowingly since the colour editor had been initialised with it.
Regards
Craig
Tags
GridView
Asked by
Craig
Top achievements
Rank 1
Answers by
Craig
Top achievements
Rank 1
Nick
Telerik team
Share this question
or