9.6 I want to...">

Windows Phone 8.1 Bind only to "Left" Margin properties

I have a static resource:

<x:Double x:Key="dOffset">9.6</x:Double>

      

I want to present this resource in a property Margin.Left

in style.

I've tried this:

  <Style x:Key="HomeButtonTextContainer" TargetType="StackPanel">
        <Setter Property="Margin">
            <Setter.Value>
                <Binding Path="Thickness">
                    <Binding.Source>
                        <local:CustomThickness Left="{StaticResource dOffset}" Top="0" Bottom="0"  Right="0" />
                    </Binding.Source>
                </Binding>
            </Setter.Value>
        </Setter>
    </Style>

      

But that won't work. I am unable to declare the thickness as a resource like below, the compiler is complaining about this.

 <Thickness x:Key="dOffset" Left="9.6" Right="0" Left="0" Top="0"></Thickness>

      

I cannot extract from the Thickness class, so I had to create a Custom that creates the Thickness (CustomThickness) class

How can I solve this?

+3


source to share


1 answer


You cannot install only TopMargin. You must set all values ​​of the Thickness instance. If you don't want to change other fields, just set them to zero.

XAML

 <Style x:Key="HomeButtonTextContainer"
               TargetType="StackPanel">
            <Setter Property="Margin"
                    Value="{Binding Source={StaticResource dOffset}, 
                    Converter={StaticResource myConverter}}">                   
            </Setter>

      



And you have to instantiate the converter class returning a Thickness instance:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var topMargin = (double)value;
        return new Thickness(0, topMargin, 0, 0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

      

Edited: Windows Phone does not support binding to a setter value. Perhaps this article will help you.

+3


source







All Articles