We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to Bind Grid Style setter to a Datacontext property

Hi,
I have found and used in your samples, the code that allows to bind the property of a Style setter to  a control:

<Style x:Key="alternatingRowStyle" TargetType="syxg:VirtualizingCellsControl">
<Setter Property="Background" >
	<Setter.Value>
		<SolidColorBrush Color="{Binding Path=Color, ElementName=alternatingRowBackgroundPicker}" />
	</Setter.Value>
</Setter>
</Style>
The above code is perfect when I have a color picker palette editor in the window hosting the grid and the style.
Now, in my application I want to let the user choose once the colors to apply to all datagrids in the applications and then use that color in all grids on all windows.
I have a configuration window where the colors are chosen and saved so that the application can access them.
In all windows of my application I've then made the color properties available in the Model class used as Datacontext for the window.
What I want to know is if is possible and how to replace the above binding to a binding to a property of the Datacontext of the window something like the code below.

<Style x:Key="alternatingRowStyle" TargetType="syxg:VirtualizingCellsControl">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="{Binding ColorAlternateRowBackground,UpdateSourceTrigger=PropertyChanged}" />
</Setter.Value>
</Setter>
</Style>
Where ColorAlternateRowBackground is a property of a class, let's call it MyWindowModel and in my code I have the following:

public MyWindowModel Model
{
	get
	{
		return mModel;
	}
	private set
	{
		mModel = value;
	}
}
public MyWindow()
{
	InitializeComponent();
Model = new MyWindowModel(); this.DataContext = Model; }
Thank you in advance
Regards
Sabrina



3 Replies

JS Jayapradha S Syncfusion Team April 7, 2016 05:59 PM UTC

Hi Sabrina,

Thank you for Contacting Syncfusion Support.

We have analyzed your requirement and you can bind the setter property for style from DataContext by using the below code ,

Code Example:

<Style x:Key="alternatingRowStyle" TargetType="Syncfusion:VirtualizingCellsControl">

            <Setter Property="Background">

                <Setter.Value>

                    <SolidColorBrush Color="{Binding Path=DataContext.ColorPickerColor,

                                     UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}" />

                </Setter.Value>

            </Setter>
</Style>



class ViewModel : INotifyPropertyChanged

    {

      

        #region Constructor

 

        public ViewModel()

        {

           Employees = PopulateCollection();

           ColorPickerColor = Colors.Green;
        }

private Color colorPickerColor;


        public Color ColorPickerColor


        {

            get { return colorPickerColor; }

            set { colorPickerColor = value; OnPropertyChanged("ColorPickerColor"); }
        }

     }



Please find the sample from the following location,
Sample Link: http://www.syncfusion.com/downloads/support/forum/123650/ze/SfGrid-946312724

Regards,
Jayapradha



SC sabrina c. April 8, 2016 08:15 AM UTC

This works perfectly,
I'd suggest you to add a small article about this to the Knowledge base, because I think that being able to simply give the users the possibility to configure UI with the colors that suit better for them is very useful.
I adopted this kind of behaviour since 1991, and none of my users ever complained having this possibility ;o)

For the record, I've a configuration window where I collect and save 5 color parameters for the grids. The colors are available application wide in a singleton class I use as Application Context wide data provider and then the colors are mapped to properties of the view model of all the windows needing them.



Sabrina


JS Jayapradha S Syncfusion Team April 11, 2016 12:59 PM UTC

Hi Sabrina,

Thank you for your update.

We are glad to know that your requirement has been met. We will add this into our KB article.

Regards,
Jayapradha


Loader.
Live Chat Icon For mobile
Up arrow icon