B
B
BadCats2017-07-06 22:59:47
WPF
BadCats, 2017-07-06 22:59:47

What am I doing wrong when creating dependency property in wpf?

I have a MainWindow.XAML file
- in which I create my own control called ControlBrush

<ControlBrush:MyBrushClass
            x:Name="NewBackground"
              CanvasBrushCLR="{Binding ElementName=canvas1, Path=Background }">
         
        </ControlBrush:MyBrushClass>

using the MyBrushClass class,
here I include it: I
xmlns:ControlBrush="clr-namespace:WpfApp1"
give this control the name x:Name="NewBackground"
Next, I access the CanvasBrushCLR CLR property,
here it is in the MyBrushClass class:
public double CanvasBrushCLR
        {
            get { return (double)GetValue(CanvasBrushProperty); }
            set { SetValue(CanvasBrushProperty, value); }
        }

and as a value I use a binding (Binding) to the canvas1 element on the form and its Background property
As for the CLR property that I use for binding, I use it when registering the CanvasBrushProperty dependency property
public static DependencyProperty CanvasBrushProperty;//имя поля
static MyBrushClass()
        {
            CanvasBrushProperty = DependencyProperty.Register("CanvasBrushCLR", typeof(double ), typeof(MyBrushClass));
        }
            //CanvasBrushCLR -ИМЯ CLR обвертки 
            //typeof(Color) - тип свойства зависимости
            //typeof(MyBrushClass) - класс в котором регистрируется свойство зависимости

Well, on the form I have two buttons and, accordingly, event handlers for them:
MyBrushClass mb;
        private void button2_Click(object sender, RoutedEventArgs e)
        {

          mb = new MyBrushClass();
          canvas1.Background.Opacity =  mb.CanvasBrushCLR; //обрабатываем binding
            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            mb = new MyBrushClass();
            mb.CanvasBrushCLR = (double)Convert.ToInt32( TB1.GetLineText(0));
        }

As you can see in one of the event handlers, I write the value from TB1 to the CLR property - TextBox on the form - i.e. I enter a string into the TextBox , and when the button is pressed, it is converted to double and assigned to the CanvasBrushCLR property.
But, when in another button I want to assign canvas1.Background.Opacity to canvas1.Background.Opacity with the already "initialized" CanvasBrushCLR property, I get an error:
Cannot set object property '#FFFFFFFF' because it is in read-only state
- although both Opacity and CanvasBrushCLR - have get and set access methods.
I specifically look at it in such detail to demonstrate that I seem to have figured out the dependency properties - that is, I have a rough idea, but I can’t fully understand where the error is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lam0x86, 2017-07-07
@BadCats

Most likely your canvas1.Background is Brushes.White. This is a singleton, and it is in the Frozen state (IsFrozen == true property), that is, it cannot be changed. Try specifying "#FFFFFF" instead of "White".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question