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. WPF
  4. Bind To DP On Control

Bind To DP On Control

Scheduled Pinned Locked Moved WPF
wpfhelpquestionwcfcom
6 Posts 4 Posters 0 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.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have created a RadioButton who's style will look similar to [this](http://3.bp.blogspot.com/-hw\_WTwyp4Fo/Tol\_TIu3jXI/AAAAAAAAABU/JjUpdLH1ecQ/s320/radio5.png). In my version the line can be top, left, right, or bottom. In the code behind I did:

    public class MaroisRadioButton : RadioButton
    {
    public enum Position
    {
    Bottom,
    Left,
    Right,
    Top
    }

    #region DP RadioMarkPosition
    public static readonly DependencyProperty RadioMarkPositionProperty =
                DependencyProperty.Register("RadioMarkPosition",
                typeof(Position),
                typeof(MaroisRadioButton),
                new PropertyMetadata(Position.Left));
    
    public Position RadioMarkPosition
    {
        get { return (Position)GetValue(RadioMarkPositionProperty); }
        set { SetValue(RadioMarkPositionProperty, value); }
    }
    #endregion
    
    #region DP RadioMarkSize
    public static readonly DependencyProperty RadioMarkSizeProperty =
                DependencyProperty.Register("RadioMarkSize",
                typeof(double),
                typeof(MaroisRadioButton),
                new PropertyMetadata(null));
    
    public double RadioMarkSize
    {
        get { return (double)GetValue(RadioMarkSizeProperty); }
        set { SetValue(RadioMarkSizeProperty, value); }
    }
    #endregion
    

    }

    Then I created style for it and in the style I want to bind the RadioMarkSize to the Width property in the XAML:

    But I can't get the binding to work. I get a binding error "BindingExpression path error: 'RadioMarkSize' property not found on 'object' ''MainWindowViewModel'" How do I bind the Width property to the DP in the code behind? Thanks

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    P B 2 Replies Last reply
    0
    • K Kevin Marois

      I have created a RadioButton who's style will look similar to [this](http://3.bp.blogspot.com/-hw\_WTwyp4Fo/Tol\_TIu3jXI/AAAAAAAAABU/JjUpdLH1ecQ/s320/radio5.png). In my version the line can be top, left, right, or bottom. In the code behind I did:

      public class MaroisRadioButton : RadioButton
      {
      public enum Position
      {
      Bottom,
      Left,
      Right,
      Top
      }

      #region DP RadioMarkPosition
      public static readonly DependencyProperty RadioMarkPositionProperty =
                  DependencyProperty.Register("RadioMarkPosition",
                  typeof(Position),
                  typeof(MaroisRadioButton),
                  new PropertyMetadata(Position.Left));
      
      public Position RadioMarkPosition
      {
          get { return (Position)GetValue(RadioMarkPositionProperty); }
          set { SetValue(RadioMarkPositionProperty, value); }
      }
      #endregion
      
      #region DP RadioMarkSize
      public static readonly DependencyProperty RadioMarkSizeProperty =
                  DependencyProperty.Register("RadioMarkSize",
                  typeof(double),
                  typeof(MaroisRadioButton),
                  new PropertyMetadata(null));
      
      public double RadioMarkSize
      {
          get { return (double)GetValue(RadioMarkSizeProperty); }
          set { SetValue(RadioMarkSizeProperty, value); }
      }
      #endregion
      

      }

      Then I created style for it and in the style I want to bind the RadioMarkSize to the Width property in the XAML:

      But I can't get the binding to work. I get a binding error "BindingExpression path error: 'RadioMarkSize' property not found on 'object' ''MainWindowViewModel'" How do I bind the Width property to the DP in the code behind? Thanks

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The DependencyProperty is on a derived RadioButton and you're attempting to read it on a Border bound to the VM DataContext. That's why your code can't see it. Why not put your derived RadioButton on there and get access to the DPs from that?

      K 1 Reply Last reply
      0
      • P Pete OHanlon

        The DependencyProperty is on a derived RadioButton and you're attempting to read it on a Border bound to the VM DataContext. That's why your code can't see it. Why not put your derived RadioButton on there and get access to the DPs from that?

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Here's the full style. The Border is part of the RadioButton Control Template's BulletDecarator:

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type mbc:MaroisRadioButton}">
        
                    <BulletDecorator Cursor="Hand">
        
                        <Border BorderBrush="LightGray"
                                BorderThickness="2"
                                Margin="2">
        
                            <Grid>
        
                                <Border Grid.Row="1"
                                        Grid.Column="0"
                                        Margin="2"
                                        Background="{DynamicResource buttonPressedBackBrush}"
                                        Width="{Binding RadioMarkSize}"
                                        HorizontalAlignment="Left"
                                        VerticalAlignment="Stretch"
                                        Name="LeftRadioMark"/>
        
                                <ContentPresenter Grid.Row="1"
                                                    Grid.Column="1"
                                                    HorizontalAlignment="Center"
                                                    VerticalAlignment="Center"
                                                    Margin="2"/>
        
                            </Grid>
        
                        </Border>
        
                    </BulletDecorator>
                     
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        Richard DeemingR 1 Reply Last reply
        0
        • K Kevin Marois

          Here's the full style. The Border is part of the RadioButton Control Template's BulletDecarator:

          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="{x:Type mbc:MaroisRadioButton}">
          
                      <BulletDecorator Cursor="Hand">
          
                          <Border BorderBrush="LightGray"
                                  BorderThickness="2"
                                  Margin="2">
          
                              <Grid>
          
                                  <Border Grid.Row="1"
                                          Grid.Column="0"
                                          Margin="2"
                                          Background="{DynamicResource buttonPressedBackBrush}"
                                          Width="{Binding RadioMarkSize}"
                                          HorizontalAlignment="Left"
                                          VerticalAlignment="Stretch"
                                          Name="LeftRadioMark"/>
          
                                  <ContentPresenter Grid.Row="1"
                                                      Grid.Column="1"
                                                      HorizontalAlignment="Center"
                                                      VerticalAlignment="Center"
                                                      Margin="2"/>
          
                              </Grid>
          
                          </Border>
          
                      </BulletDecorator>
                       
                  </ControlTemplate>
              </Setter.Value>
          </Setter>
          

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Try using TemplateBinding instead.

          <Border
          Grid.Row="1"
          Grid.Column="0"
          Margin="2"
          Background="{DynamicResource buttonPressedBackBrush}"
          Width="{TemplateBinding RadioMarkSize}"
          HorizontalAlignment="Left"
          VerticalAlignment="Stretch"
          Name="LeftRadioMark"
          />

          TemplateBinding Markup Extension | Microsoft Docs[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          K 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Try using TemplateBinding instead.

            <Border
            Grid.Row="1"
            Grid.Column="0"
            Margin="2"
            Background="{DynamicResource buttonPressedBackBrush}"
            Width="{TemplateBinding RadioMarkSize}"
            HorizontalAlignment="Left"
            VerticalAlignment="Stretch"
            Name="LeftRadioMark"
            />

            TemplateBinding Markup Extension | Microsoft Docs[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            That did it. Thanks! ya learnt me something

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            1 Reply Last reply
            0
            • K Kevin Marois

              I have created a RadioButton who's style will look similar to [this](http://3.bp.blogspot.com/-hw\_WTwyp4Fo/Tol\_TIu3jXI/AAAAAAAAABU/JjUpdLH1ecQ/s320/radio5.png). In my version the line can be top, left, right, or bottom. In the code behind I did:

              public class MaroisRadioButton : RadioButton
              {
              public enum Position
              {
              Bottom,
              Left,
              Right,
              Top
              }

              #region DP RadioMarkPosition
              public static readonly DependencyProperty RadioMarkPositionProperty =
                          DependencyProperty.Register("RadioMarkPosition",
                          typeof(Position),
                          typeof(MaroisRadioButton),
                          new PropertyMetadata(Position.Left));
              
              public Position RadioMarkPosition
              {
                  get { return (Position)GetValue(RadioMarkPositionProperty); }
                  set { SetValue(RadioMarkPositionProperty, value); }
              }
              #endregion
              
              #region DP RadioMarkSize
              public static readonly DependencyProperty RadioMarkSizeProperty =
                          DependencyProperty.Register("RadioMarkSize",
                          typeof(double),
                          typeof(MaroisRadioButton),
                          new PropertyMetadata(null));
              
              public double RadioMarkSize
              {
                  get { return (double)GetValue(RadioMarkSizeProperty); }
                  set { SetValue(RadioMarkSizeProperty, value); }
              }
              #endregion
              

              }

              Then I created style for it and in the style I want to bind the RadioMarkSize to the Width property in the XAML:

              But I can't get the binding to work. I get a binding error "BindingExpression path error: 'RadioMarkSize' property not found on 'object' ''MainWindowViewModel'" How do I bind the Width property to the DP in the code behind? Thanks

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              B Offline
              B Offline
              BenScharbach
              wrote on last edited by
              #6

              Did you set the DataContext to your custom create class called "MaroisRadioButton":confused: I believe in the code-behind you need to set the DataContext to this custom class so XAML engine can locate this custom dependency property.

              Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach

              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