Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. WPF - Changing Fill property for group of shapes

WPF - Changing Fill property for group of shapes

Scheduled Pinned Locked Moved C#
wpfcsharphelp
5 Posts 2 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shawn Horton
    wrote on last edited by
    #1

    I have a group of shapes in a WPF UserControl, and I want them to all have the same color selected by the user at runtime. I thought I could create a DynamicResource in place of a hard coded fill value and change the value through the code behind class. This would let me create a property box with a color picker and allow the user to change colors. I cannot get this to work. Below are a couple examples of what I have tried. Usercontrol XAML file: Code behind class for the control. public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } static UserControl1() { FillColorProperty = DependencyProperty.Register("SolidBrush", typeof(SolidColorBrush), typeof(UserControl1), new UIPropertyMetadata(Brushes.PowderBlue)); } static DependencyProperty FillColorProperty; /// /// PLACEHOLDER /// public SolidColorBrush SolidBrush { get { return (SolidColorBrush)base.GetValue(FillColorProperty); } set { base.SetValue(FillColorProperty, value); } } } I have tried so many ways to do this, that I am grasping at straws now. Any help would be appreciated.

    C 1 Reply Last reply
    0
    • S Shawn Horton

      I have a group of shapes in a WPF UserControl, and I want them to all have the same color selected by the user at runtime. I thought I could create a DynamicResource in place of a hard coded fill value and change the value through the code behind class. This would let me create a property box with a color picker and allow the user to change colors. I cannot get this to work. Below are a couple examples of what I have tried. Usercontrol XAML file: Code behind class for the control. public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } static UserControl1() { FillColorProperty = DependencyProperty.Register("SolidBrush", typeof(SolidColorBrush), typeof(UserControl1), new UIPropertyMetadata(Brushes.PowderBlue)); } static DependencyProperty FillColorProperty; /// /// PLACEHOLDER /// public SolidColorBrush SolidBrush { get { return (SolidColorBrush)base.GetValue(FillColorProperty); } set { base.SetValue(FillColorProperty, value); } } } I have tried so many ways to do this, that I am grasping at straws now. Any help would be appreciated.

      C Offline
      C Offline
      CKnig
      wrote on last edited by
      #2

      So you define a Resouce and a dependend property with the same Name - why don't you just stay with the Dependend Property? (I can't even see where you use the "SolidBrush"-Resource). Maybe you can post some code where you want to use this Brush because I guess your mistake lies there.

      S 1 Reply Last reply
      0
      • C CKnig

        So you define a Resouce and a dependend property with the same Name - why don't you just stay with the Dependend Property? (I can't even see where you use the "SolidBrush"-Resource). Maybe you can post some code where you want to use this Brush because I guess your mistake lies there.

        S Offline
        S Offline
        Shawn Horton
        wrote on last edited by
        #3

        I guess I am not being to helpful to myself. I just want to create a custom control with two ellipses with a variable fill color that can be changed at runtime. The control would have a property dialog with a color picker, that would change the ellipse fill color to the selected color. That is all I need. I know I could set the fill color of each ellipse independently by modifying the Fill property in the codebehind, but I was looking for a way to just change a single "color" variable and save myself some work. I have been trying every combination of resource, dynamic property, and anything else I can find, and I posted some code somewhere between changes. Thanks for any help you can give. Shawn

        <UserControl x:Class="WpfCustomControlLibrary2.UserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300">
        <UserControl.Resources>
        <SolidColorBrush x:Key="SolidBrush" Color="#FF00FFFF"/>
        <UserControl.Resources>
        <Grid>
        <Ellipse Height="27" HorizontalAlignment="Left" Margin="50,42,0,0" Name="ellipse1" Stroke="Black" Fill="{DynamicResource SolidBrush}" VerticalAlignment="Top" Width="46" />
        <Ellipse Height="27" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ellipse2" Stroke="Black" Fill="{DynamicResource SolidBrush}" VerticalAlignment="Top" Width="46" />
        </Grid>
        <UserControl>

        In the control's code behind file:

        SolidColorBrush _FillColor; ** //I want this variable to control the DynamicResource SolidBrush**

        //Property to change fill color
        public SolidColorBrush SolidBrush
        {
        get
        {
        return (SolidColorBrush) _FillColor;
        }
        set
        {
        _FillColor = value;
        }
        }

        I got an answer from the MSDN Forums

        <UserControl x:Class="WpfCustomControlLibrary2.UserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Height="300" Width="300">
        <Grid>
        <Ellipse Height="27" HorizontalAlignment="Left" Margin="50,42,0,0" Name="ellipse1" Stroke="Black" Fill="{Binding Path=SolidBrush}" VerticalAlignment="Top" Width="46" />

        C 1 Reply Last reply
        0
        • S Shawn Horton

          I guess I am not being to helpful to myself. I just want to create a custom control with two ellipses with a variable fill color that can be changed at runtime. The control would have a property dialog with a color picker, that would change the ellipse fill color to the selected color. That is all I need. I know I could set the fill color of each ellipse independently by modifying the Fill property in the codebehind, but I was looking for a way to just change a single "color" variable and save myself some work. I have been trying every combination of resource, dynamic property, and anything else I can find, and I posted some code somewhere between changes. Thanks for any help you can give. Shawn

          <UserControl x:Class="WpfCustomControlLibrary2.UserControl1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Height="300" Width="300">
          <UserControl.Resources>
          <SolidColorBrush x:Key="SolidBrush" Color="#FF00FFFF"/>
          <UserControl.Resources>
          <Grid>
          <Ellipse Height="27" HorizontalAlignment="Left" Margin="50,42,0,0" Name="ellipse1" Stroke="Black" Fill="{DynamicResource SolidBrush}" VerticalAlignment="Top" Width="46" />
          <Ellipse Height="27" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ellipse2" Stroke="Black" Fill="{DynamicResource SolidBrush}" VerticalAlignment="Top" Width="46" />
          </Grid>
          <UserControl>

          In the control's code behind file:

          SolidColorBrush _FillColor; ** //I want this variable to control the DynamicResource SolidBrush**

          //Property to change fill color
          public SolidColorBrush SolidBrush
          {
          get
          {
          return (SolidColorBrush) _FillColor;
          }
          set
          {
          _FillColor = value;
          }
          }

          I got an answer from the MSDN Forums

          <UserControl x:Class="WpfCustomControlLibrary2.UserControl1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          DataContext="{Binding RelativeSource={RelativeSource Self}}"
          Height="300" Width="300">
          <Grid>
          <Ellipse Height="27" HorizontalAlignment="Left" Margin="50,42,0,0" Name="ellipse1" Stroke="Black" Fill="{Binding Path=SolidBrush}" VerticalAlignment="Top" Width="46" />

          C Offline
          C Offline
          CKnig
          wrote on last edited by
          #4

          Shawn Horton wrote:

          And change the code behind to: public static readonly DependencyProperty SolidBrushProperty = DependencyProperty.Register("SolidBrush", typeof(SolidColorBrush), typeof(UserControl1), new UIPropertyMetadata(null)); public SolidColorBrush SolidBrush { get { return (SolidColorBrush)GetValue(SolidBrushProperty); } set { SetValue(SolidBrushProperty, value); } }

          This one should work but maybe you should change the DependencyProperty code to something like FrameworkPropertyMetadata metaBrush= new FrameworkPropertyMetadata( Brushes.Blue, FrameworkPropertyMetadataOptions.None); SolidBrushProperty = DependencyProperty.Register("SolidBrush", typeof(SolidColorBrush), typeof(UserControl1), metaBrush); (I normaly define this in the static constructor of the class) As you can see the only difference is the use of the initialised (Blue brush) FrameworkPropertyMetadata instead of a empty UIPropertyMetadata. The rest looks fine to me.

          S 1 Reply Last reply
          0
          • C CKnig

            Shawn Horton wrote:

            And change the code behind to: public static readonly DependencyProperty SolidBrushProperty = DependencyProperty.Register("SolidBrush", typeof(SolidColorBrush), typeof(UserControl1), new UIPropertyMetadata(null)); public SolidColorBrush SolidBrush { get { return (SolidColorBrush)GetValue(SolidBrushProperty); } set { SetValue(SolidBrushProperty, value); } }

            This one should work but maybe you should change the DependencyProperty code to something like FrameworkPropertyMetadata metaBrush= new FrameworkPropertyMetadata( Brushes.Blue, FrameworkPropertyMetadataOptions.None); SolidBrushProperty = DependencyProperty.Register("SolidBrush", typeof(SolidColorBrush), typeof(UserControl1), metaBrush); (I normaly define this in the static constructor of the class) As you can see the only difference is the use of the initialised (Blue brush) FrameworkPropertyMetadata instead of a empty UIPropertyMetadata. The rest looks fine to me.

            S Offline
            S Offline
            Shawn Horton
            wrote on last edited by
            #5

            CKnig wrote:

            FrameworkPropertyMetadata metaBrush= new FrameworkPropertyMetadata( Brushes.Blue, FrameworkPropertyMetadataOptions.None); SolidBrushProperty = DependencyProperty.Register("SolidBrush", typeof(SolidColorBrush), typeof(UserControl1), metaBrush); (I normaly define this in the static constructor of the class) As you can see the only difference is the use of the initialised (Blue brush) FrameworkPropertyMetadata instead of a empty UIPropertyMetadata. The rest looks fine to me.

            I did that last night, the control works great. Thanks for the help. Shawn

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups