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 <-
wpf mvvm binding margin thickness
add a comment |
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 <-
wpf mvvm binding margin thickness
Possible duplicate of Wpf Binding two variables to Margin
– Peregrine
2 days ago
@Peregrine yes it is , should i delete my question?
– Guardian
yesterday
add a comment |
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 <-
wpf mvvm binding margin thickness
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
wpf mvvm binding margin thickness
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
add a comment |
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
add a comment |
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;
}
}
add a comment |
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;
}
}
add a comment |
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;
}
}
add a comment |
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;
}
}
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;
}
}
edited 2 days ago
answered 2 days ago
Satish Pai
938
938
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Possible duplicate of Wpf Binding two variables to Margin
– Peregrine
2 days ago
@Peregrine yes it is , should i delete my question?
– Guardian
yesterday