2014-11-06 90 views
3

我有一個靜態的資源:的Windows手機8.1綁定只「左」 Margin屬性

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

我想這個資源assing在樣式的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> 

我不能從類派生的厚度,所以我不得不做出一個自定義的是建立一個厚度(CustomThickness類)

我怎麼能解決這個問題?

回答

3

您不能只設置TopMargin。您應該設置厚度實例的所有值。如果您不想更改其他邊距,請將它們設置爲零。

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不支持在二傳手的價值結合。也許this文章幫助你。

+0

這是我在我的問題相同。我得到這個錯誤:未能分配給屬性'%0'。 [Line:45 Position:54] – Herno 2014-11-06 19:08:47

+0

如果您提供有關錯誤消息的更多詳細信息,我可以說WP不支持setter值的綁定。您可以在編輯的答案中查看解決方法。 – 2014-11-06 19:21:34