Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in wpf:
I created a slider and bind it to window background.here is the code:
C#
Value="{Binding Path=Background.Color.B, ElementName=windowhelp, Mode=TwoWay}"

now when I run window and change the slider value , window background does not ghange!
how can I change window background when I change the slider value?
Posted
Updated 1-Sep-13 23:00pm
v4
Comments
Pheonyx 2-Sep-13 4:12am    
If you are using a slider then you will need some sort of converter I would have thought. What values does your Slider output, if they are not "Colors" then you will need to build a converter that converts the integer value output of your slider to a colour so that it can be bound to the background property.
e121 2-Sep-13 4:46am    
No!
When I bind slider output there are colors!

listen.my code works but not up to date!
in fact when I change slider , The background does not change.
for ghanging window background color I have to change slider.Then change window size so that window background color changes!

I want to change window background color when I change slider quickly.

1 solution

Hi,

This is my solution for the problem:

MainWindow.cs:

XML
<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525" x:Name="window" removed="LightBlue">
    <Window.Resources>
        <local:BrushToIntConverter x:Key="BrushToIntConverter" />
    </Window.Resources>
    <DockPanel>
        <Slider DockPanel.Dock="Top" Minimum="0" Maximum="256" Value="{Binding Path=Background, ElementName=window, Mode=TwoWay, Converter={StaticResource  ResourceKey=BrushToIntConverter}, ConverterParameter=R}"/>
        <Slider DockPanel.Dock="Top" Minimum="0" Maximum="256" Value="{Binding Path=Background, ElementName=window, Mode=TwoWay, Converter={StaticResource  ResourceKey=BrushToIntConverter}, ConverterParameter=G}"/>
        <Slider DockPanel.Dock="Top" Minimum="0" Maximum="256" Value="{Binding Path=Background, ElementName=window, Mode=TwoWay, Converter={StaticResource  ResourceKey=BrushToIntConverter}, ConverterParameter=B}"/>
    </DockPanel>
</Window>


Three silders for R,G,B.

And the Brush to Int Converter:

C#
[ValueConversion(typeof(System.Windows.Media.Brush), typeof(int))]
public class BrushToIntConverter : IValueConverter
{
    protected System.Windows.Media.Color m_Color = System.Windows.Media.Colors.White;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        System.Windows.Media.SolidColorBrush brush = value as System.Windows.Media.SolidColorBrush;

        m_Color = brush.Color;

        switch (parameter as string)
        {
            case "R":
                return m_Color.R;
            case "G":
                return m_Color.G;
            case "B":
                return m_Color.B;
        }

        return 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int intValue = (int)Math.Round((double)value);

        byte r = m_Color.R;
        byte g = m_Color.G;
        byte b = m_Color.B;

        switch (parameter as string)
        {
            case "R":
                r = (byte)intValue;
                break;
            case "G":
                g = (byte)intValue;
                break;
            case "B":
                b = (byte)intValue;
                break;
        }

        m_Color = new System.Windows.Media.Color() { R = r, G = g, B = b, A = m_Color.A };

        return new System.Windows.Media.SolidColorBrush(m_Color);
    }
}
 
Share this answer
 
v2
Comments
M.Kamran Asim 21-Apr-14 4:28am    
Nice work. this is perfect solution. 2 thing you must know about this scenario.
1. slider contains double value.
2. Windows background is actually Brush (Solid, Linear, Radial), So you can't bind Brush value to double value directly. For that you need converter. first comment provided by pheonyx is right.
Shai Vashdi 21-Apr-14 10:12am    
Thank you, Yes your are right! The brush is a solid brush & the slider value is double. I just wanted to show him how to easily convert these values.

Best Regards,

Shai

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900