我有多个位于Grid中的列表框。每个列表框由GridSplitter分隔。
每个ListBoxItem都有一个显示多行数据的datatemplate。 我想要的是,当列表框调整大小时(通过GridSplitter),ListBoxItem DataTemplate可以相应地更新(显示不同级别的细节,例如当大小小于数字时,我将隐藏一些内容)。
我现在正在ListBox上监听SizeChanged,然后在我的自定义ListBox上设置一个属性,DataTemplate中的控件将绑定到要显示或隐藏的属性。
例如,我的ListBox上有一个属性)
MediumSize = (ActualWidth > 200 && ActualWidth < 400);
在我的Xaml中,某些控件的Visibility将绑定到此属性。
有效。 但这很愚蠢,因为显然我只能使用一个“大小”级别。
还有更好的方法吗?
谢谢
答案 0 :(得分:0)
<Window.Resources>
<local:HeightToTemplateConverter x:Key="HeightToTemplateConverter"/>
<DataTemplate x:Key="three">
<StackPanel>
<TextBlock Text="{Binding FName}"/>
<TextBlock Text="{Binding SName}"/>
<TextBlock Text="{Binding Age}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="two">
<StackPanel >
<TextBlock Text="{Binding FName}"/>
<TextBlock Text="{Binding SName}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="one">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FName}"/>
<TextBlock Text="{Binding SName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid Height="300" Width="500">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<ListBox Grid.Row="0" ItemTemplate="{Binding ActualHeight, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource HeightToTemplateConverter}, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding MyColl}" />
<GridSplitter Grid.Row="1" Height="5" ResizeDirection="Rows" Background="Red" VerticalAlignment="Center" HorizontalAlignment="Stretch" />
<ListBox ItemTemplate="{Binding ActualHeight, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource HeightToTemplateConverter}, UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" ItemsSource="{Binding MyColl}" />
</Grid>
代码背后的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
public class Model
{
private string fname;
public string FName
{
get { return fname; }
set { fname = value; }
}
private string sname;
public string SName
{
get { return sname; }
set { sname = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
}
public class ViewModel
{
private ObservableCollection<Model> mycoll;
public ObservableCollection<Model> MyColl
{
get { return mycoll; }
set { mycoll = value; }
}
public ViewModel()
{
MyColl = new ObservableCollection<Model>();
MyColl.Add(new Model { FName = "Muthu", SName = "Kumar", Age = 23 });
MyColl.Add(new Model { FName = "Muthu", SName = "Kumar", Age = 23 });
MyColl.Add(new Model { FName = "Muthu", SName = "Kumar", Age = 23 }); MyColl.Add(new Model { FName = "Muthu", SName = "Kumar", Age = 23 });
MyColl.Add(new Model { FName = "Muthu", SName = "Kumar", Age = 23 });
MyColl.Add(new Model { FName = "Muthu", SName = "Kumar", Age = 23 });
}
}
public class HeightToTemplateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (Double.Parse(value.ToString()) >= 100)
return App.Current.MainWindow.Resources["three"] as DataTemplate;
if (Double.Parse(value.ToString()) <= 100 && Double.Parse(value.ToString()) >= 60)
return App.Current.MainWindow.Resources["two"] as DataTemplate;
if (Double.Parse(value.ToString()) < 60)
return App.Current.MainWindow.Resources["one"] as DataTemplate;
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这只是一个试用版,让我知道它是否有帮助。
由于 库马尔