define style in code

essamce 621 Reputation points
2020-11-21T14:40:31.383+00:00
  < Canvas.Resources >
    < Style  TargetType = "{x:Type Path}" BasedOn="{StaticResource "{x:Type Path}>
         < Setter Property = "Stroke" Value = "Black" />
            < Setter Property = "StrokeThickness" Value = "0.7" />
               < Setter Property = "Canvas.Left" >
                    < Setter.Value >
                      < MultiBinding Converter = "{StaticResource multiply2Numbers}" >
                          < Binding Path = "RenderTransformOrigin.X" RelativeSource = "{RelativeSource Self}" />
                          < Binding Path = "Scale" RelativeSource = "{RelativeSource AncestorType={x:Type Canvas}}" />
                      </ MultiBinding >
                  </ Setter.Value >
              </ Setter >
              < Setter Property = "Canvas.Top" >
                  < Setter.Value >
                      < MultiBinding Converter = "{StaticResource multiply2Numbers}" >
                          < Binding Path = "RenderTransformOrigin.Y" RelativeSource = "{RelativeSource Self}" />
                          < Binding Path = "Scale" RelativeSource = "{RelativeSource AncestorType={x:Type Canvas}}" />
                      </ MultiBinding >
                  </ Setter.Value >
              </ Setter >
          </ Style >
      </ Canvas.Resources >

how to create the above resources in code?
we need this class to work as Custom Control,
Note: the code is for a base class and we want all derived classes children (of type Path) to apply this style.
Note: prop Scale is a DependenceyProperty we already defined.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,316 Reputation points
    2020-11-22T07:48:00.98+00:00

    Hi,
    I hope my code helps you. In the past you have never rated my answers as helpful.

    public class CanvasWithItemsSource : Canvas
      {
        public CanvasWithItemsSource()
        {
          Style st = new Style(typeof(Path));
          st.Setters.Add(new Setter(Path.StrokeProperty, Brushes.Black));
          st.Setters.Add(new Setter(Path.StrokeThicknessProperty, 0.7));
          IMultiValueConverter converter = new Multiply2Numbers();
          MultiBinding binding1 = new MultiBinding() { Converter = converter };
          binding1.Bindings.Add(new Binding("RenderTransformOrigin.X") { RelativeSource = RelativeSource.Self });
          binding1.Bindings.Add(new Binding("Scale") { RelativeSource = new RelativeSource() { AncestorType = typeof(Canvas) } });
          st.Setters.Add(new Setter(Canvas.LeftProperty, binding1));
          MultiBinding binding2 = new MultiBinding() { Converter = converter };
          binding2.Bindings.Add(new Binding("RenderTransformOrigin.Y") { RelativeSource = RelativeSource.Self });
          binding2.Bindings.Add(new Binding("Scale") { RelativeSource = new RelativeSource() { AncestorType = typeof(Canvas) } });
          st.Setters.Add(new Setter(Canvas.TopProperty, binding2));
          this.Resources.Add(typeof(Path), st);
        }
    ......
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful