ListBox多列时无法设置按照比例来适应屏幕宽度,不知道有没有更好的办法?
<local:ScreenResolution x:Key="res_screenResolution"/>
数据模板:
<DataTemplate x:Key="DT_order">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Path=ScreenWidth, Source={StaticResource res_screenResolution}}"/>
<ColumnDefinition Width="{Binding Path=ScreenWidth, Source={StaticResource res_screenResolution}}"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" FontSize="20"/>
<TextBlock Text="{Binding Age}" FontSize="20" Grid.Column="1"/>
</Grid>
</DataTemplate>
由于Width="*"设置无效,所以想用绑定来实现,但是在listbox加载前无法获取到屏幕宽度(不知道有没有更好获取屏幕宽度的办法)。
获取屏幕分辨率类:
public class ScreenResolution : INotifyPropertyChanged
{
private double mScreenWidth = 0;
private double mScreenHeight = 0;
public event PropertyChangedEventHandler PropertyChanged;//
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public double ScreenWidth
{
get
{
return mScreenWidth;//绑定是成功的,此函数被多次调用了
}
set
{
if (mScreenWidth != value)
{
mScreenWidth = value;
NotifyPropertyChanged("ScreenWidth");
}
}
}
public double ScreenHeight
{
get
{
return mScreenHeight / 2;
}
set
{
if (mScreenHeight != value)
{
mScreenHeight = value;
NotifyPropertyChanged("ScreenHeight");
}
}
}
public void SetScreenResolution(double width, double height)
{
ScreenWidth = width;
ScreenHeight = height;
}
}
在主窗口Page_SizeChanged中调用SetScreenResolution(double width, double height)方法,但是此时列表已经加载完成了,晚了。
------解决思路----------------------
难道LISTBOX 最新加载的控件吗,建议选来一个SLASHVIEW,拿到数据后再初始化这些控件。