【发布时间】:2017-06-13 14:19:04
【问题描述】:
我在ItemsControl 中托管了一个包含 64 个UserControl 的列表,DataContext 是一个对象数组。然后,UserControl 的单个实例的DataContext 成为对象的实例。
对象有一个名为Exists 的布尔变量,这是一个DataTemplate 触发器,用于确定是否显示Usercontrol。
我使用Uniformgrid 来显示列表,但我遇到了一些奇怪的行为。 Usercontrol 不调整大小。见附图。如果我改用StackPanel,它就可以正常工作。但我想改用UnifromGrid。
这里是代码 - 只有 4 个对象将 Exist 变量设置为 true。
<Grid Grid.Row="1" Grid.Column="1" x:Name="gridSome" Background="#FF5AC1F1">
<Viewbox>
<ItemsControl ItemsSource="{Binding SomeVM.SomeModel.SomeArray}"
Margin="15" HorizontalAlignment="Center" VerticalContentAlignment="Center">
<ItemsControl.ItemTemplate>
<DataTemplate>
<tensioner:UCView Margin="5"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Exists}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<!--<StackPanel IsItemsHost="true"/> This works-->
<UniformGrid Columns="1"/> <!-- This does not work-->
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Viewbox>
</Grid>
-----更新-----
//SSCCE 主窗口
<Window x:Class="WpfAppItemIssue.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppItemIssue"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<!--<Viewbox>-->
<ItemsControl ItemsSource="{Binding Model.Cars}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="ABC"></TextBox>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding exists}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<!--</Viewbox>-->
</Grid>
</Window>
主视图模型
using System.ComponentModel;
namespace WpfAppItemIssue
{
class MainViewModel:INotifyPropertyChanged
{
public MainViewModel()
{
Model = new MainModel();
}
private MainModel model;
public MainModel Model
{
get
{
return model;
}
set
{
model = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}
型号
namespace WpfAppItemIssue
{
class MainModel
{
public Car[] Cars { get; set; }
public MainModel()
{
Cars = new Car[64];
for (int i = 0; i < Cars.Length; i++)
{
Cars[i] = new Car(i);
}
}
}
internal class Car
{
public int someVal { get; set; }
public bool exists { get; set; }
public Car(int someVal)
{
this.someVal = someVal;
if (someVal < 5) //Just enable few items for debug
{
exists = true;
}
else
{
exists = false;
}
}
}
}
查看附件图片:
图 1 显示了设计视图。为什么不调整用户控件的大小? 图 2 显示执行时。为什么不调整用户控件的大小? 图 3 显示了任何调整大小事件。控件正在正确调整大小。
【问题讨论】:
-
如果
StackPanel更适合您,为什么还需要UniformGrid? -
XAML 中
Viewbox的用途是什么? -
我想缩放
ItemsControl以适应Grid。而且,现在我想缩放UserControls以适应多列中的UniformGrid。 -
我真的不明白你为什么需要
Viewbox来适应UserControl的UniformGrid单元格。默认情况下,放置在Grid或UniformGrid内的所有元素的垂直/水平对齐设置为Stretch。我什至浪费了一些时间来编写示例应用程序,我在ItemsControl中的所有元素都成功缩放以占用网格单元格的所有空间。请显示UCView的 XAML。 -
也可以尝试将
Viewbox的Stretch属性设置为Uniform或Fill等。
标签: wpf itemscontrol stackpanel uniformgrid