WPF: Binding Margin/Thickness Left and Top Property











up vote
0
down vote

favorite












I got a problem with bindings(i know why am i becoming this exception but dunno how to solve the problem).



I have tried this piece of code.



  <TextBlock HorizontalAlignment="Left" >
<TextBlock.Margin>
<Thickness Left="{Binding POSX.Value, Converter={StaticResource DPIConverter}}"
Top="{Binding POSY.Value, Converter={StaticResource DPIConverter}}"/>
</TextBlock.Margin>
</TextBlock>


Im getting an exception where it says that, u cant bind thickness [LEFT], [TOP] properties. (ik why : cause those properties are not Dependency Property)



Thanks for ur help.



Edit : In case you didnt understand what am i trying to reach



-> I want to bind Left and Top Properties of Margin <-










share|improve this question
























  • Possible duplicate of Wpf Binding two variables to Margin
    – Peregrine
    2 days ago










  • @Peregrine yes it is , should i delete my question?
    – Guardian
    yesterday















up vote
0
down vote

favorite












I got a problem with bindings(i know why am i becoming this exception but dunno how to solve the problem).



I have tried this piece of code.



  <TextBlock HorizontalAlignment="Left" >
<TextBlock.Margin>
<Thickness Left="{Binding POSX.Value, Converter={StaticResource DPIConverter}}"
Top="{Binding POSY.Value, Converter={StaticResource DPIConverter}}"/>
</TextBlock.Margin>
</TextBlock>


Im getting an exception where it says that, u cant bind thickness [LEFT], [TOP] properties. (ik why : cause those properties are not Dependency Property)



Thanks for ur help.



Edit : In case you didnt understand what am i trying to reach



-> I want to bind Left and Top Properties of Margin <-










share|improve this question
























  • Possible duplicate of Wpf Binding two variables to Margin
    – Peregrine
    2 days ago










  • @Peregrine yes it is , should i delete my question?
    – Guardian
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I got a problem with bindings(i know why am i becoming this exception but dunno how to solve the problem).



I have tried this piece of code.



  <TextBlock HorizontalAlignment="Left" >
<TextBlock.Margin>
<Thickness Left="{Binding POSX.Value, Converter={StaticResource DPIConverter}}"
Top="{Binding POSY.Value, Converter={StaticResource DPIConverter}}"/>
</TextBlock.Margin>
</TextBlock>


Im getting an exception where it says that, u cant bind thickness [LEFT], [TOP] properties. (ik why : cause those properties are not Dependency Property)



Thanks for ur help.



Edit : In case you didnt understand what am i trying to reach



-> I want to bind Left and Top Properties of Margin <-










share|improve this question















I got a problem with bindings(i know why am i becoming this exception but dunno how to solve the problem).



I have tried this piece of code.



  <TextBlock HorizontalAlignment="Left" >
<TextBlock.Margin>
<Thickness Left="{Binding POSX.Value, Converter={StaticResource DPIConverter}}"
Top="{Binding POSY.Value, Converter={StaticResource DPIConverter}}"/>
</TextBlock.Margin>
</TextBlock>


Im getting an exception where it says that, u cant bind thickness [LEFT], [TOP] properties. (ik why : cause those properties are not Dependency Property)



Thanks for ur help.



Edit : In case you didnt understand what am i trying to reach



-> I want to bind Left and Top Properties of Margin <-







wpf mvvm binding margin thickness






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









Guardian

215




215












  • Possible duplicate of Wpf Binding two variables to Margin
    – Peregrine
    2 days ago










  • @Peregrine yes it is , should i delete my question?
    – Guardian
    yesterday


















  • Possible duplicate of Wpf Binding two variables to Margin
    – Peregrine
    2 days ago










  • @Peregrine yes it is , should i delete my question?
    – Guardian
    yesterday
















Possible duplicate of Wpf Binding two variables to Margin
– Peregrine
2 days ago




Possible duplicate of Wpf Binding two variables to Margin
– Peregrine
2 days ago












@Peregrine yes it is , should i delete my question?
– Guardian
yesterday




@Peregrine yes it is , should i delete my question?
– Guardian
yesterday












1 Answer
1






active

oldest

votes

















up vote
1
down vote













That's right you can't bind Left,Top,right or Bottom, because they are not dependency property. They are CLR property. DependencyProperty is wrapper to CLR Property.



Class which defines a dependency property must be inherited from the DependencyObject class. Thickness is a class which is not inherited from DependencyObject class. But the Margin is from TextBlock, which is inherited from FrameworkElement , and FrameworkElement inherited from UIElement, and UIElement is inherited from Visual which inherits from DependencyObject class.



What you can bind is Margin, since Margin is a dependency Property registered in FrameworkElement Class.



You can change your Xaml like this
(sample code)



<TextBlock HorizontalAlignment="Left" Margin="{Binding POS, Converter={StaticResource DPIConverter}}" >


Below is the converter code, where we can send the whole thickness



 public class DPIConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{ // your code inside Ivalue
// based pn some value send left and right value. other's can zero
// or which ever value you need.
int x = POS.PosX.Value;
int y = POS.PoxY.Value;

return new Thickness(System.Convert.ToDouble(x), System.Convert.ToDouble(y), 0, 0);
}

public object ConvertBack(object value, System.Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}





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%2f53350240%2fwpf-binding-margin-thickness-left-and-top-property%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













    That's right you can't bind Left,Top,right or Bottom, because they are not dependency property. They are CLR property. DependencyProperty is wrapper to CLR Property.



    Class which defines a dependency property must be inherited from the DependencyObject class. Thickness is a class which is not inherited from DependencyObject class. But the Margin is from TextBlock, which is inherited from FrameworkElement , and FrameworkElement inherited from UIElement, and UIElement is inherited from Visual which inherits from DependencyObject class.



    What you can bind is Margin, since Margin is a dependency Property registered in FrameworkElement Class.



    You can change your Xaml like this
    (sample code)



    <TextBlock HorizontalAlignment="Left" Margin="{Binding POS, Converter={StaticResource DPIConverter}}" >


    Below is the converter code, where we can send the whole thickness



     public class DPIConverter : IValueConverter
    {
    public object Convert(object value, System.Type targetType, object parameter,
    System.Globalization.CultureInfo culture)
    { // your code inside Ivalue
    // based pn some value send left and right value. other's can zero
    // or which ever value you need.
    int x = POS.PosX.Value;
    int y = POS.PoxY.Value;

    return new Thickness(System.Convert.ToDouble(x), System.Convert.ToDouble(y), 0, 0);
    }

    public object ConvertBack(object value, System.Type targetType,
    object parameter, System.Globalization.CultureInfo culture)
    {
    return null;
    }
    }





    share|improve this answer



























      up vote
      1
      down vote













      That's right you can't bind Left,Top,right or Bottom, because they are not dependency property. They are CLR property. DependencyProperty is wrapper to CLR Property.



      Class which defines a dependency property must be inherited from the DependencyObject class. Thickness is a class which is not inherited from DependencyObject class. But the Margin is from TextBlock, which is inherited from FrameworkElement , and FrameworkElement inherited from UIElement, and UIElement is inherited from Visual which inherits from DependencyObject class.



      What you can bind is Margin, since Margin is a dependency Property registered in FrameworkElement Class.



      You can change your Xaml like this
      (sample code)



      <TextBlock HorizontalAlignment="Left" Margin="{Binding POS, Converter={StaticResource DPIConverter}}" >


      Below is the converter code, where we can send the whole thickness



       public class DPIConverter : IValueConverter
      {
      public object Convert(object value, System.Type targetType, object parameter,
      System.Globalization.CultureInfo culture)
      { // your code inside Ivalue
      // based pn some value send left and right value. other's can zero
      // or which ever value you need.
      int x = POS.PosX.Value;
      int y = POS.PoxY.Value;

      return new Thickness(System.Convert.ToDouble(x), System.Convert.ToDouble(y), 0, 0);
      }

      public object ConvertBack(object value, System.Type targetType,
      object parameter, System.Globalization.CultureInfo culture)
      {
      return null;
      }
      }





      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        That's right you can't bind Left,Top,right or Bottom, because they are not dependency property. They are CLR property. DependencyProperty is wrapper to CLR Property.



        Class which defines a dependency property must be inherited from the DependencyObject class. Thickness is a class which is not inherited from DependencyObject class. But the Margin is from TextBlock, which is inherited from FrameworkElement , and FrameworkElement inherited from UIElement, and UIElement is inherited from Visual which inherits from DependencyObject class.



        What you can bind is Margin, since Margin is a dependency Property registered in FrameworkElement Class.



        You can change your Xaml like this
        (sample code)



        <TextBlock HorizontalAlignment="Left" Margin="{Binding POS, Converter={StaticResource DPIConverter}}" >


        Below is the converter code, where we can send the whole thickness



         public class DPIConverter : IValueConverter
        {
        public object Convert(object value, System.Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
        { // your code inside Ivalue
        // based pn some value send left and right value. other's can zero
        // or which ever value you need.
        int x = POS.PosX.Value;
        int y = POS.PoxY.Value;

        return new Thickness(System.Convert.ToDouble(x), System.Convert.ToDouble(y), 0, 0);
        }

        public object ConvertBack(object value, System.Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
        {
        return null;
        }
        }





        share|improve this answer














        That's right you can't bind Left,Top,right or Bottom, because they are not dependency property. They are CLR property. DependencyProperty is wrapper to CLR Property.



        Class which defines a dependency property must be inherited from the DependencyObject class. Thickness is a class which is not inherited from DependencyObject class. But the Margin is from TextBlock, which is inherited from FrameworkElement , and FrameworkElement inherited from UIElement, and UIElement is inherited from Visual which inherits from DependencyObject class.



        What you can bind is Margin, since Margin is a dependency Property registered in FrameworkElement Class.



        You can change your Xaml like this
        (sample code)



        <TextBlock HorizontalAlignment="Left" Margin="{Binding POS, Converter={StaticResource DPIConverter}}" >


        Below is the converter code, where we can send the whole thickness



         public class DPIConverter : IValueConverter
        {
        public object Convert(object value, System.Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
        { // your code inside Ivalue
        // based pn some value send left and right value. other's can zero
        // or which ever value you need.
        int x = POS.PosX.Value;
        int y = POS.PoxY.Value;

        return new Thickness(System.Convert.ToDouble(x), System.Convert.ToDouble(y), 0, 0);
        }

        public object ConvertBack(object value, System.Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
        {
        return null;
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        Satish Pai

        938




        938






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350240%2fwpf-binding-margin-thickness-left-and-top-property%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







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin