1。使用Grid布局中的“比重”的方式:
与Android中的weight比较像,但在xaml中为star,对于宽度来说,就设置column,
计算方法:
a.percent=a.star/(a.star+b.star+…);
2。 数据绑定:Binding:
个人觉得这个还是比较方便的,唯一的麻烦是你要自己写个Convert类来实现比例的转化。
另外,好像还要求绑定的元素对象要有确定的Width值,不能指定为Auto。否则会变成NAN。
Width="{Binding ElementName=Window,Path=Width,Converter={StaticResource percent}}"
public class Percent : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double )value * 0.5;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
最终的效果:
资源地址:http://download.csdn.net/detail/u014271114/9010731
第一次在CSDN上面写博文,而且又是刚刚接触到WPF,新手还请大家指教。欢迎评论。