将UI元素的绑定宽度与另一个UI元素的宽度的百分比
问题描述:
基于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%。
答
所以我结束了使用转换器来传递一个数字。只能想,而不是我为总数的其余部分减去一个固定宽度的百分比,因为我有一个数字。
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);
}