Windows Phone 8.1仅绑定“左”边距属性

我有一个静态资源:

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

我想将此资源关联到Style中的Margin.Left属性.

我尝试了这个:

  <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>

但这行不通.
我不能像以下那样将Thickness声明为资源,编译器对此有所抱怨.

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

我不能从Thickness类派生,因此我必须创建一个构建Thickness的Custom(CustomThickness类)

我该如何解决?

解决方法:

您不能仅设置TopMargin.您应该设置Thickness实例的所有值.如果您不想更改其他边距,只需将其设置为零即可.

XAML

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

并且您应该创建返回厚度实例的转换器类:

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();
    }
}

编辑:
Windows Phone不支持setter值的绑定.也许this文章可以帮助您.

上一篇:css3box-sizing


下一篇:绑定变量到文本块