2013-04-10 67 views
1

基於this question,但略有不同。將UI元素的綁定寬度與另一個UI元素的寬度的百分比

我不確定這是否可能。有什麼辦法將UI元素的寬度鏈接到另一個元素的寬度百分比

例如:

<ScrollViewer x:Name="scrollviewerWrapper"> 

    <TextBlock x:Name="textblock" 
       Width="{Binding Path=Width, [[[ % of ]]] ElementName=scrollviewerWrapper}" /> 

</ScrollViewer> 

所以,如果我的ScrollViewer是100px的,我希望我的TextBlock成爲其中的= 60像素(該ScrollViewer中的寬度是動態的)60%。

回答

3

所以我結束了使用轉換器來傳遞一個數字。只能想,而不是我爲總數的其餘部分減去一個固定寬度的百分比,因爲我有一個數字。

XAML:

Width="{Binding Path=ActualWidth, ElementName=exampleElement, Converter={StaticResource MyConverter}, ConverterParameter={StaticResource ResourceKey=actionAreaWidth}}"> 

參數轉換:

<clr:Double x:Key="actionAreaWidth">150</clr:Double> 

和轉換器本身:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 

      double percentage = 100; 
      double.TryParse(parameter.ToString(), out percentage); 

      var actualWidth = (double)value; 
      return actualWidth * (percentage/100); 
     } 
相關問題