Setting a background color with Brush









up vote
0
down vote

favorite












So I have created a design with sliders, scrollBars and IntegerUpDowns which all binded togheter move at the same time and have the same value. I have for of each, one for each ARGB. I have a stackPanel in the center which BackgroundColor should change while any of the Tools is modified.



As far as I thought about it, I just need to know one of the tools values in order to set the background through the data they deliver... But how can I implement that ?



So far I have coded this:



public partial class MainWindow : Window

SolidColorBrush brush;

public MainWindow()

InitializeComponent();

brush = new SolidColorBrush();
brush.Color = Color.FromArgb(0, 0, 0, 0);
stkColor.Background = brush;


private void scbScrollA_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)

brush.Color.A = scbScrollA.Value(); //doesn't work


private void scbScrollR_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)




private void scbScrollG_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)




private void scbScrollB_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)






As I said, just by knowing the scroll data, because I have used Bindings with all the other tools, I could manage to set the color and refresh every single time any data is modified.



The bindings I have:



<xctk:IntegerUpDown Grid.Column="7" Grid.Row="0" x:Name="iudB" Increment="10" Minimum="0" Maximum="255"
Value="Binding ElementName=scbScrollB, Path=Value"
/>

<ScrollBar Grid.Column="1" Grid.Row="6" Grid.RowSpan="2" Width="Auto" Orientation="Horizontal"
Minimum="0" Maximum="255" x:Name="scbScrollB"
Scroll="scbScrollB_Scroll" SmallChange="1"
LargeChange="10" Value="Binding ElementName=sliderB, Path=Value"
/>

<Slider
Grid.Column="6" Grid.Row="1" Grid.RowSpan="1"
Orientation="Vertical"
LargeChange="10"
Maximum="255"
SmallChange="1"
TickPlacement="TopLeft"
Minimum="0"
TickFrequency="25"
x:Name="sliderB"
/>


The slider doesn't have binding because as far as I know they are bi-directional. None of them gives error.










share|improve this question























  • Color is a struct, not a class. You must assign a new Color value like this: brush.Color = Color.FromArgb((byte)scbScrollA.Value, brush.Color.R, brush.Color.G, brush.Color.B);
    – Clemens
    Nov 10 at 20:11











  • It's WPF, not WinForms. Use data bindings, not event handlers ;-) You could for example use a <MultiBinding> for the StackPanel.Background property. The MultiBinding would bind against the 4 Value properties of your ScrollBar controls. The MultiBinding would also have to use a multi-value converter which converts the 4 values from the MultiBinding into a color/brush. You would need to implement this multi-value converter (based on IMultiValueConverter). If you don't know about MultiBinding, a web search will surely yield plenty of blog posts and tutorials...
    – elgonzo
    Nov 10 at 20:21











  • @Clemens How can I achieve what you are saying? I have changed the constructor: 'brush = new SolidColorBrush(); brush.Color = Color.FromArgb((byte)scbScrollA.Value, (byte)scbScrollR.Value, (byte)scbScrollG.Value, (byte)scbScrollB.Value); stkColor.Background = brush;'
    – Raül
    Nov 10 at 20:26










  • Put that code in the Scroll event handler.
    – Clemens
    Nov 10 at 20:34






  • 1




    The Scroll event is only called when you move the ScrollBar's Thumb. Use its ValueChanged event instead. Besides that, better bind all the control properties to a view model, and update the color there. Return the Color value by a public property.
    – Clemens
    Nov 10 at 20:59















up vote
0
down vote

favorite












So I have created a design with sliders, scrollBars and IntegerUpDowns which all binded togheter move at the same time and have the same value. I have for of each, one for each ARGB. I have a stackPanel in the center which BackgroundColor should change while any of the Tools is modified.



As far as I thought about it, I just need to know one of the tools values in order to set the background through the data they deliver... But how can I implement that ?



So far I have coded this:



public partial class MainWindow : Window

SolidColorBrush brush;

public MainWindow()

InitializeComponent();

brush = new SolidColorBrush();
brush.Color = Color.FromArgb(0, 0, 0, 0);
stkColor.Background = brush;


private void scbScrollA_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)

brush.Color.A = scbScrollA.Value(); //doesn't work


private void scbScrollR_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)




private void scbScrollG_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)




private void scbScrollB_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)






As I said, just by knowing the scroll data, because I have used Bindings with all the other tools, I could manage to set the color and refresh every single time any data is modified.



The bindings I have:



<xctk:IntegerUpDown Grid.Column="7" Grid.Row="0" x:Name="iudB" Increment="10" Minimum="0" Maximum="255"
Value="Binding ElementName=scbScrollB, Path=Value"
/>

<ScrollBar Grid.Column="1" Grid.Row="6" Grid.RowSpan="2" Width="Auto" Orientation="Horizontal"
Minimum="0" Maximum="255" x:Name="scbScrollB"
Scroll="scbScrollB_Scroll" SmallChange="1"
LargeChange="10" Value="Binding ElementName=sliderB, Path=Value"
/>

<Slider
Grid.Column="6" Grid.Row="1" Grid.RowSpan="1"
Orientation="Vertical"
LargeChange="10"
Maximum="255"
SmallChange="1"
TickPlacement="TopLeft"
Minimum="0"
TickFrequency="25"
x:Name="sliderB"
/>


The slider doesn't have binding because as far as I know they are bi-directional. None of them gives error.










share|improve this question























  • Color is a struct, not a class. You must assign a new Color value like this: brush.Color = Color.FromArgb((byte)scbScrollA.Value, brush.Color.R, brush.Color.G, brush.Color.B);
    – Clemens
    Nov 10 at 20:11











  • It's WPF, not WinForms. Use data bindings, not event handlers ;-) You could for example use a <MultiBinding> for the StackPanel.Background property. The MultiBinding would bind against the 4 Value properties of your ScrollBar controls. The MultiBinding would also have to use a multi-value converter which converts the 4 values from the MultiBinding into a color/brush. You would need to implement this multi-value converter (based on IMultiValueConverter). If you don't know about MultiBinding, a web search will surely yield plenty of blog posts and tutorials...
    – elgonzo
    Nov 10 at 20:21











  • @Clemens How can I achieve what you are saying? I have changed the constructor: 'brush = new SolidColorBrush(); brush.Color = Color.FromArgb((byte)scbScrollA.Value, (byte)scbScrollR.Value, (byte)scbScrollG.Value, (byte)scbScrollB.Value); stkColor.Background = brush;'
    – Raül
    Nov 10 at 20:26










  • Put that code in the Scroll event handler.
    – Clemens
    Nov 10 at 20:34






  • 1




    The Scroll event is only called when you move the ScrollBar's Thumb. Use its ValueChanged event instead. Besides that, better bind all the control properties to a view model, and update the color there. Return the Color value by a public property.
    – Clemens
    Nov 10 at 20:59













up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I have created a design with sliders, scrollBars and IntegerUpDowns which all binded togheter move at the same time and have the same value. I have for of each, one for each ARGB. I have a stackPanel in the center which BackgroundColor should change while any of the Tools is modified.



As far as I thought about it, I just need to know one of the tools values in order to set the background through the data they deliver... But how can I implement that ?



So far I have coded this:



public partial class MainWindow : Window

SolidColorBrush brush;

public MainWindow()

InitializeComponent();

brush = new SolidColorBrush();
brush.Color = Color.FromArgb(0, 0, 0, 0);
stkColor.Background = brush;


private void scbScrollA_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)

brush.Color.A = scbScrollA.Value(); //doesn't work


private void scbScrollR_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)




private void scbScrollG_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)




private void scbScrollB_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)






As I said, just by knowing the scroll data, because I have used Bindings with all the other tools, I could manage to set the color and refresh every single time any data is modified.



The bindings I have:



<xctk:IntegerUpDown Grid.Column="7" Grid.Row="0" x:Name="iudB" Increment="10" Minimum="0" Maximum="255"
Value="Binding ElementName=scbScrollB, Path=Value"
/>

<ScrollBar Grid.Column="1" Grid.Row="6" Grid.RowSpan="2" Width="Auto" Orientation="Horizontal"
Minimum="0" Maximum="255" x:Name="scbScrollB"
Scroll="scbScrollB_Scroll" SmallChange="1"
LargeChange="10" Value="Binding ElementName=sliderB, Path=Value"
/>

<Slider
Grid.Column="6" Grid.Row="1" Grid.RowSpan="1"
Orientation="Vertical"
LargeChange="10"
Maximum="255"
SmallChange="1"
TickPlacement="TopLeft"
Minimum="0"
TickFrequency="25"
x:Name="sliderB"
/>


The slider doesn't have binding because as far as I know they are bi-directional. None of them gives error.










share|improve this question















So I have created a design with sliders, scrollBars and IntegerUpDowns which all binded togheter move at the same time and have the same value. I have for of each, one for each ARGB. I have a stackPanel in the center which BackgroundColor should change while any of the Tools is modified.



As far as I thought about it, I just need to know one of the tools values in order to set the background through the data they deliver... But how can I implement that ?



So far I have coded this:



public partial class MainWindow : Window

SolidColorBrush brush;

public MainWindow()

InitializeComponent();

brush = new SolidColorBrush();
brush.Color = Color.FromArgb(0, 0, 0, 0);
stkColor.Background = brush;


private void scbScrollA_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)

brush.Color.A = scbScrollA.Value(); //doesn't work


private void scbScrollR_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)




private void scbScrollG_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)




private void scbScrollB_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)






As I said, just by knowing the scroll data, because I have used Bindings with all the other tools, I could manage to set the color and refresh every single time any data is modified.



The bindings I have:



<xctk:IntegerUpDown Grid.Column="7" Grid.Row="0" x:Name="iudB" Increment="10" Minimum="0" Maximum="255"
Value="Binding ElementName=scbScrollB, Path=Value"
/>

<ScrollBar Grid.Column="1" Grid.Row="6" Grid.RowSpan="2" Width="Auto" Orientation="Horizontal"
Minimum="0" Maximum="255" x:Name="scbScrollB"
Scroll="scbScrollB_Scroll" SmallChange="1"
LargeChange="10" Value="Binding ElementName=sliderB, Path=Value"
/>

<Slider
Grid.Column="6" Grid.Row="1" Grid.RowSpan="1"
Orientation="Vertical"
LargeChange="10"
Maximum="255"
SmallChange="1"
TickPlacement="TopLeft"
Minimum="0"
TickFrequency="25"
x:Name="sliderB"
/>


The slider doesn't have binding because as far as I know they are bi-directional. None of them gives error.







wpf brush






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 20:59

























asked Nov 10 at 20:05









Raül

1259




1259











  • Color is a struct, not a class. You must assign a new Color value like this: brush.Color = Color.FromArgb((byte)scbScrollA.Value, brush.Color.R, brush.Color.G, brush.Color.B);
    – Clemens
    Nov 10 at 20:11











  • It's WPF, not WinForms. Use data bindings, not event handlers ;-) You could for example use a <MultiBinding> for the StackPanel.Background property. The MultiBinding would bind against the 4 Value properties of your ScrollBar controls. The MultiBinding would also have to use a multi-value converter which converts the 4 values from the MultiBinding into a color/brush. You would need to implement this multi-value converter (based on IMultiValueConverter). If you don't know about MultiBinding, a web search will surely yield plenty of blog posts and tutorials...
    – elgonzo
    Nov 10 at 20:21











  • @Clemens How can I achieve what you are saying? I have changed the constructor: 'brush = new SolidColorBrush(); brush.Color = Color.FromArgb((byte)scbScrollA.Value, (byte)scbScrollR.Value, (byte)scbScrollG.Value, (byte)scbScrollB.Value); stkColor.Background = brush;'
    – Raül
    Nov 10 at 20:26










  • Put that code in the Scroll event handler.
    – Clemens
    Nov 10 at 20:34






  • 1




    The Scroll event is only called when you move the ScrollBar's Thumb. Use its ValueChanged event instead. Besides that, better bind all the control properties to a view model, and update the color there. Return the Color value by a public property.
    – Clemens
    Nov 10 at 20:59

















  • Color is a struct, not a class. You must assign a new Color value like this: brush.Color = Color.FromArgb((byte)scbScrollA.Value, brush.Color.R, brush.Color.G, brush.Color.B);
    – Clemens
    Nov 10 at 20:11











  • It's WPF, not WinForms. Use data bindings, not event handlers ;-) You could for example use a <MultiBinding> for the StackPanel.Background property. The MultiBinding would bind against the 4 Value properties of your ScrollBar controls. The MultiBinding would also have to use a multi-value converter which converts the 4 values from the MultiBinding into a color/brush. You would need to implement this multi-value converter (based on IMultiValueConverter). If you don't know about MultiBinding, a web search will surely yield plenty of blog posts and tutorials...
    – elgonzo
    Nov 10 at 20:21











  • @Clemens How can I achieve what you are saying? I have changed the constructor: 'brush = new SolidColorBrush(); brush.Color = Color.FromArgb((byte)scbScrollA.Value, (byte)scbScrollR.Value, (byte)scbScrollG.Value, (byte)scbScrollB.Value); stkColor.Background = brush;'
    – Raül
    Nov 10 at 20:26










  • Put that code in the Scroll event handler.
    – Clemens
    Nov 10 at 20:34






  • 1




    The Scroll event is only called when you move the ScrollBar's Thumb. Use its ValueChanged event instead. Besides that, better bind all the control properties to a view model, and update the color there. Return the Color value by a public property.
    – Clemens
    Nov 10 at 20:59
















Color is a struct, not a class. You must assign a new Color value like this: brush.Color = Color.FromArgb((byte)scbScrollA.Value, brush.Color.R, brush.Color.G, brush.Color.B);
– Clemens
Nov 10 at 20:11





Color is a struct, not a class. You must assign a new Color value like this: brush.Color = Color.FromArgb((byte)scbScrollA.Value, brush.Color.R, brush.Color.G, brush.Color.B);
– Clemens
Nov 10 at 20:11













It's WPF, not WinForms. Use data bindings, not event handlers ;-) You could for example use a <MultiBinding> for the StackPanel.Background property. The MultiBinding would bind against the 4 Value properties of your ScrollBar controls. The MultiBinding would also have to use a multi-value converter which converts the 4 values from the MultiBinding into a color/brush. You would need to implement this multi-value converter (based on IMultiValueConverter). If you don't know about MultiBinding, a web search will surely yield plenty of blog posts and tutorials...
– elgonzo
Nov 10 at 20:21





It's WPF, not WinForms. Use data bindings, not event handlers ;-) You could for example use a <MultiBinding> for the StackPanel.Background property. The MultiBinding would bind against the 4 Value properties of your ScrollBar controls. The MultiBinding would also have to use a multi-value converter which converts the 4 values from the MultiBinding into a color/brush. You would need to implement this multi-value converter (based on IMultiValueConverter). If you don't know about MultiBinding, a web search will surely yield plenty of blog posts and tutorials...
– elgonzo
Nov 10 at 20:21













@Clemens How can I achieve what you are saying? I have changed the constructor: 'brush = new SolidColorBrush(); brush.Color = Color.FromArgb((byte)scbScrollA.Value, (byte)scbScrollR.Value, (byte)scbScrollG.Value, (byte)scbScrollB.Value); stkColor.Background = brush;'
– Raül
Nov 10 at 20:26




@Clemens How can I achieve what you are saying? I have changed the constructor: 'brush = new SolidColorBrush(); brush.Color = Color.FromArgb((byte)scbScrollA.Value, (byte)scbScrollR.Value, (byte)scbScrollG.Value, (byte)scbScrollB.Value); stkColor.Background = brush;'
– Raül
Nov 10 at 20:26












Put that code in the Scroll event handler.
– Clemens
Nov 10 at 20:34




Put that code in the Scroll event handler.
– Clemens
Nov 10 at 20:34




1




1




The Scroll event is only called when you move the ScrollBar's Thumb. Use its ValueChanged event instead. Besides that, better bind all the control properties to a view model, and update the color there. Return the Color value by a public property.
– Clemens
Nov 10 at 20:59





The Scroll event is only called when you move the ScrollBar's Thumb. Use its ValueChanged event instead. Besides that, better bind all the control properties to a view model, and update the color there. Return the Color value by a public property.
– Clemens
Nov 10 at 20:59













1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Create a view model with four byte properties for the four components of a Color, and a Color property for the resulting color value.



public class ViewModel : INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;

private byte alpha;
private byte red;
private byte green;
private byte blue;

private void NotifyPropertyChanged(string propertyName)

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));


private void SetColorComponent(ref byte field, byte value, string propertyName)

if (field != value)

field = value;
NotifyPropertyChanged(propertyName);
NotifyPropertyChanged(nameof(Color));



public byte Alpha

get return alpha;
set SetColorComponent(ref alpha, value, nameof(Alpha));


public byte Red

get return red;
set SetColorComponent(ref red, value, nameof(Red));


public byte Green

get return green;
set SetColorComponent(ref green, value, nameof(Green));


public byte Blue

get return blue;
set SetColorComponent(ref blue, value, nameof(Blue));


public Color Color

get return Color.FromArgb(alpha, red, green, blue);




Assign an instance of the view model to the DataContext of the window and bind the Sliders, ScrollBars, etc. to the view model properties.



<Window ...>
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<StackPanel.Background>
<SolidColorBrush Color="Binding Color"/>
</StackPanel.Background>

<Slider Minimum="0" Maximum="255" Value="Binding Alpha"/>
<Slider Minimum="0" Maximum="255" Value="Binding Red"/>
<Slider Minimum="0" Maximum="255" Value="Binding Green"/>
<Slider Minimum="0" Maximum="255" Value="Binding Blue"/>

<ScrollBar Minimum="0" Maximum="255" Value="Binding Alpha" Orientation="Horizontal"/>
<ScrollBar Minimum="0" Maximum="255" Value="Binding Red" Orientation="Horizontal"/>
<ScrollBar Minimum="0" Maximum="255" Value="Binding Green" Orientation="Horizontal"/>
<ScrollBar Minimum="0" Maximum="255" Value="Binding Blue" Orientation="Horizontal"/>
</StackPanel>
</Window>





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242950%2fsetting-a-stackpanel-background-color-with-brush%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Create a view model with four byte properties for the four components of a Color, and a Color property for the resulting color value.



    public class ViewModel : INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private byte alpha;
    private byte red;
    private byte green;
    private byte blue;

    private void NotifyPropertyChanged(string propertyName)

    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));


    private void SetColorComponent(ref byte field, byte value, string propertyName)

    if (field != value)

    field = value;
    NotifyPropertyChanged(propertyName);
    NotifyPropertyChanged(nameof(Color));



    public byte Alpha

    get return alpha;
    set SetColorComponent(ref alpha, value, nameof(Alpha));


    public byte Red

    get return red;
    set SetColorComponent(ref red, value, nameof(Red));


    public byte Green

    get return green;
    set SetColorComponent(ref green, value, nameof(Green));


    public byte Blue

    get return blue;
    set SetColorComponent(ref blue, value, nameof(Blue));


    public Color Color

    get return Color.FromArgb(alpha, red, green, blue);




    Assign an instance of the view model to the DataContext of the window and bind the Sliders, ScrollBars, etc. to the view model properties.



    <Window ...>
    <Window.DataContext>
    <local:ViewModel/>
    </Window.DataContext>
    <StackPanel>
    <StackPanel.Background>
    <SolidColorBrush Color="Binding Color"/>
    </StackPanel.Background>

    <Slider Minimum="0" Maximum="255" Value="Binding Alpha"/>
    <Slider Minimum="0" Maximum="255" Value="Binding Red"/>
    <Slider Minimum="0" Maximum="255" Value="Binding Green"/>
    <Slider Minimum="0" Maximum="255" Value="Binding Blue"/>

    <ScrollBar Minimum="0" Maximum="255" Value="Binding Alpha" Orientation="Horizontal"/>
    <ScrollBar Minimum="0" Maximum="255" Value="Binding Red" Orientation="Horizontal"/>
    <ScrollBar Minimum="0" Maximum="255" Value="Binding Green" Orientation="Horizontal"/>
    <ScrollBar Minimum="0" Maximum="255" Value="Binding Blue" Orientation="Horizontal"/>
    </StackPanel>
    </Window>





    share|improve this answer


























      up vote
      1
      down vote



      accepted










      Create a view model with four byte properties for the four components of a Color, and a Color property for the resulting color value.



      public class ViewModel : INotifyPropertyChanged

      public event PropertyChangedEventHandler PropertyChanged;

      private byte alpha;
      private byte red;
      private byte green;
      private byte blue;

      private void NotifyPropertyChanged(string propertyName)

      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));


      private void SetColorComponent(ref byte field, byte value, string propertyName)

      if (field != value)

      field = value;
      NotifyPropertyChanged(propertyName);
      NotifyPropertyChanged(nameof(Color));



      public byte Alpha

      get return alpha;
      set SetColorComponent(ref alpha, value, nameof(Alpha));


      public byte Red

      get return red;
      set SetColorComponent(ref red, value, nameof(Red));


      public byte Green

      get return green;
      set SetColorComponent(ref green, value, nameof(Green));


      public byte Blue

      get return blue;
      set SetColorComponent(ref blue, value, nameof(Blue));


      public Color Color

      get return Color.FromArgb(alpha, red, green, blue);




      Assign an instance of the view model to the DataContext of the window and bind the Sliders, ScrollBars, etc. to the view model properties.



      <Window ...>
      <Window.DataContext>
      <local:ViewModel/>
      </Window.DataContext>
      <StackPanel>
      <StackPanel.Background>
      <SolidColorBrush Color="Binding Color"/>
      </StackPanel.Background>

      <Slider Minimum="0" Maximum="255" Value="Binding Alpha"/>
      <Slider Minimum="0" Maximum="255" Value="Binding Red"/>
      <Slider Minimum="0" Maximum="255" Value="Binding Green"/>
      <Slider Minimum="0" Maximum="255" Value="Binding Blue"/>

      <ScrollBar Minimum="0" Maximum="255" Value="Binding Alpha" Orientation="Horizontal"/>
      <ScrollBar Minimum="0" Maximum="255" Value="Binding Red" Orientation="Horizontal"/>
      <ScrollBar Minimum="0" Maximum="255" Value="Binding Green" Orientation="Horizontal"/>
      <ScrollBar Minimum="0" Maximum="255" Value="Binding Blue" Orientation="Horizontal"/>
      </StackPanel>
      </Window>





      share|improve this answer
























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Create a view model with four byte properties for the four components of a Color, and a Color property for the resulting color value.



        public class ViewModel : INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private byte alpha;
        private byte red;
        private byte green;
        private byte blue;

        private void NotifyPropertyChanged(string propertyName)

        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));


        private void SetColorComponent(ref byte field, byte value, string propertyName)

        if (field != value)

        field = value;
        NotifyPropertyChanged(propertyName);
        NotifyPropertyChanged(nameof(Color));



        public byte Alpha

        get return alpha;
        set SetColorComponent(ref alpha, value, nameof(Alpha));


        public byte Red

        get return red;
        set SetColorComponent(ref red, value, nameof(Red));


        public byte Green

        get return green;
        set SetColorComponent(ref green, value, nameof(Green));


        public byte Blue

        get return blue;
        set SetColorComponent(ref blue, value, nameof(Blue));


        public Color Color

        get return Color.FromArgb(alpha, red, green, blue);




        Assign an instance of the view model to the DataContext of the window and bind the Sliders, ScrollBars, etc. to the view model properties.



        <Window ...>
        <Window.DataContext>
        <local:ViewModel/>
        </Window.DataContext>
        <StackPanel>
        <StackPanel.Background>
        <SolidColorBrush Color="Binding Color"/>
        </StackPanel.Background>

        <Slider Minimum="0" Maximum="255" Value="Binding Alpha"/>
        <Slider Minimum="0" Maximum="255" Value="Binding Red"/>
        <Slider Minimum="0" Maximum="255" Value="Binding Green"/>
        <Slider Minimum="0" Maximum="255" Value="Binding Blue"/>

        <ScrollBar Minimum="0" Maximum="255" Value="Binding Alpha" Orientation="Horizontal"/>
        <ScrollBar Minimum="0" Maximum="255" Value="Binding Red" Orientation="Horizontal"/>
        <ScrollBar Minimum="0" Maximum="255" Value="Binding Green" Orientation="Horizontal"/>
        <ScrollBar Minimum="0" Maximum="255" Value="Binding Blue" Orientation="Horizontal"/>
        </StackPanel>
        </Window>





        share|improve this answer














        Create a view model with four byte properties for the four components of a Color, and a Color property for the resulting color value.



        public class ViewModel : INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private byte alpha;
        private byte red;
        private byte green;
        private byte blue;

        private void NotifyPropertyChanged(string propertyName)

        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));


        private void SetColorComponent(ref byte field, byte value, string propertyName)

        if (field != value)

        field = value;
        NotifyPropertyChanged(propertyName);
        NotifyPropertyChanged(nameof(Color));



        public byte Alpha

        get return alpha;
        set SetColorComponent(ref alpha, value, nameof(Alpha));


        public byte Red

        get return red;
        set SetColorComponent(ref red, value, nameof(Red));


        public byte Green

        get return green;
        set SetColorComponent(ref green, value, nameof(Green));


        public byte Blue

        get return blue;
        set SetColorComponent(ref blue, value, nameof(Blue));


        public Color Color

        get return Color.FromArgb(alpha, red, green, blue);




        Assign an instance of the view model to the DataContext of the window and bind the Sliders, ScrollBars, etc. to the view model properties.



        <Window ...>
        <Window.DataContext>
        <local:ViewModel/>
        </Window.DataContext>
        <StackPanel>
        <StackPanel.Background>
        <SolidColorBrush Color="Binding Color"/>
        </StackPanel.Background>

        <Slider Minimum="0" Maximum="255" Value="Binding Alpha"/>
        <Slider Minimum="0" Maximum="255" Value="Binding Red"/>
        <Slider Minimum="0" Maximum="255" Value="Binding Green"/>
        <Slider Minimum="0" Maximum="255" Value="Binding Blue"/>

        <ScrollBar Minimum="0" Maximum="255" Value="Binding Alpha" Orientation="Horizontal"/>
        <ScrollBar Minimum="0" Maximum="255" Value="Binding Red" Orientation="Horizontal"/>
        <ScrollBar Minimum="0" Maximum="255" Value="Binding Green" Orientation="Horizontal"/>
        <ScrollBar Minimum="0" Maximum="255" Value="Binding Blue" Orientation="Horizontal"/>
        </StackPanel>
        </Window>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 8:16

























        answered Nov 11 at 8:41









        Clemens

        86.8k884166




        86.8k884166



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242950%2fsetting-a-stackpanel-background-color-with-brush%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto