Simon482
asked on
WPF Binding to Class in XAML
Hello,
The users of one of my WPF Browser Applications have complained that it looks too boring and have requested a bit of colour. I thought that, rather than colour it myself, it would be a nice touch to give them the ability to create their own colour scheme.
Users now have the ability to select one or two colours and can choose that they be applied to the page backgrounds as either a SolidColourBrush or a LinearGradientBrush.
I have created a class called UserColours that has four properties:
Colour1
Colour2
ColourStyle
Gradient
the first two are SolidColourBrushes, ColourStyle is a string value that will be either "Solid Colour" or "Linear Gradient" and Gradient is a LinearGradientBrush
I have set up INotifyPropertyChanged on this class so that PropertyChanged is fired when ColourStyle is changed by the user.
I also have a class of 'User' which includes the property ColourSheme, which is of the 'UserColours' type.
My intention is to bind the background property of each page to the 'ColourStyle' property (using a Style in my Application XAML) and then use an IValueConverter to return the relevant brush.
I am almost there with this but am having a problem with the binding.
Ordinarily I would bind from the code behind, but in this instance I thought it would be more sensible to bind within the XAML as this is a binding that I will need on every page.
The XAML of my Application.xaml includes:
but I cannot work out how to reference the 'ColourStyle' property in the XAML (I would need to bind to CurrentUser, an instance of the User class, so the property path would be 'CurrentUser.ColourScheme. ColourStyl e')
Is this possible? and how?
Many thanks
Simon
The users of one of my WPF Browser Applications have complained that it looks too boring and have requested a bit of colour. I thought that, rather than colour it myself, it would be a nice touch to give them the ability to create their own colour scheme.
Users now have the ability to select one or two colours and can choose that they be applied to the page backgrounds as either a SolidColourBrush or a LinearGradientBrush.
I have created a class called UserColours that has four properties:
Colour1
Colour2
ColourStyle
Gradient
the first two are SolidColourBrushes, ColourStyle is a string value that will be either "Solid Colour" or "Linear Gradient" and Gradient is a LinearGradientBrush
I have set up INotifyPropertyChanged on this class so that PropertyChanged is fired when ColourStyle is changed by the user.
I also have a class of 'User' which includes the property ColourSheme, which is of the 'UserColours' type.
My intention is to bind the background property of each page to the 'ColourStyle' property (using a Style in my Application XAML) and then use an IValueConverter to return the relevant brush.
I am almost there with this but am having a problem with the binding.
Ordinarily I would bind from the code behind, but in this instance I thought it would be more sensible to bind within the XAML as this is a binding that I will need on every page.
The XAML of my Application.xaml includes:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="clr-namespace:CoSecApp"
StartupUri="/PagesContentHolders/MainContentHolder.xaml">
<Application.Resources>
<Local:StyleToBrushConverter x:Key="ColourStyleToBrush"/>
</Application.Resources>
<Style x:Key="PageBackgroundStyle" TargetType="Page" >
<Setter Property="Background" Value="{Binding Converter={StaticResource ColourStyleToBrush}}"/>
</Style>
</Application>
but I cannot work out how to reference the 'ColourStyle' property in the XAML (I would need to bind to CurrentUser, an instance of the User class, so the property path would be 'CurrentUser.ColourScheme.
Is this possible? and how?
Many thanks
Simon
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER