我的ItemsControl有一个奇怪的问题。它始终没有显示第一个项目。我不确定,但我怀疑它可能与使用 ICollectionView 有关?
我怀疑因为取决于 SortDirection ,它总是隐藏第一项。
例如,如果我有a,b,c,d,e,f,g和ASCENDING,我得到b,c,d,e,f,g。如果DESCENDING,我得到f,e,d,c,b,a。
我绑定了我的代码:
private ICollectionView _collectionView;
public ICollectionView IngredientsOrModifiers
{
get
{
_collectionView = _collectionView ?? CollectionViewSource.GetDefaultView(_ingredientsOrModifiers);
if (!_collectionView.SortDescriptions.Any())
{
_collectionView.SortDescriptions.Add(new SortDescription(nameof(CheckIngredientOrModifierViewModel.LowercaseCheckTitle), ListSortDirection.Ascending));
}
if (!_collectionView.GroupDescriptions.Any())
{
_collectionView.GroupDescriptions.Add(new PropertyGroupDescription(null, IngredientGroupConverter.Default));
}
return _collectionView;
}
}
我的ItemsControl是:
<ItemsControl Grid.Row="2"
Grid.ColumnSpan="3"
Margin="5,0,0,0"
ItemsSource="{Binding IngredientsOrModifiers.Groups}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="subitemButton"
BorderThickness="0"
FocusVisualStyle="{x:Null}"
Margin="0,2"
HorizontalContentAlignment="Stretch"
Background="Transparent"
Command="{Binding Path=Items[0].CheckIfShouldShowComplimentaryItemDialogCommand}"
CommandParameter="{Binding Path=Items[0]}">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background"
Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="43" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Items[0].LowercaseCheckTitle}"
Style="{StaticResource CartSubItemTitleStyle}"
FontFamily="{Binding Path=Items[0].IsComplimentary, Converter={x:Static converters2:BooleanToFontFamilyConverter.Default}}"
FontWeight="{Binding Path=Items[0].IsComplimentary, Converter={x:Static converters2:BooleanToFontWeightConverter.Default}}"
x:Name="titleField" />
<Button Grid.Column="1"
x:Name="removeButton"
Style="{StaticResource RemoveSubItemButtonStyle}"
Command="{Binding RemoveIngredientOrModifierCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
CommandParameter="{Binding Items[0]}"
Visibility="Collapsed" />
<ItemsControl Grid.Row="1"
Grid.ColumnSpan="2"
Margin="5,0,0,0"
ItemsSource="{Binding Items[0].ItemModifiers}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="43" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title, Converter={x:Static converters:StringCaseConverter.ToLower}}"
Style="{StaticResource CartSubItemTitleStyle}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Button>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsCheckOut, RelativeSource={RelativeSource AncestorType={x:Type controls:ShoppingCartControl}}}"
Value="False">
<DataTrigger.Setters>
<Setter Property="MinHeight"
TargetName="subitemButton"
Value="44" />
</DataTrigger.Setters>
</DataTrigger>
<DataTrigger Binding="{Binding ItemCount, Converter={x:Static converters:CountToBooleanConverter.Default}, ConverterParameter=1}" Value="True">
<DataTrigger.Setters>
<Setter Property="Text" TargetName="titleField">
<Setter.Value>
<MultiBinding Converter="{x:Static converters2:StringFormatMultiConverter.Default}" ConverterParameter="{}{0} ({1})" UpdateSourceTrigger="PropertyChanged">
<Binding Path="Items[0].LowercaseCheckTitle" />
<Binding Path="ItemCount" />
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger.Setters>
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=CanRemoveItems, RelativeSource={RelativeSource AncestorType={x:Type controls:ShoppingCartControl}}}" Value="True" />
<Condition Binding="{Binding Path=IsCheckOut, RelativeSource={RelativeSource AncestorType={x:Type controls:ShoppingCartControl}}}" Value="False" />
<Condition Binding="{Binding Path=Items[0].CanRemove}" Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="removeButton" Property="Visibility" Value="Visible" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
修改
这是转换器:
public class IngredientGroupConverter : IValueConverter
{
public static IngredientGroupConverter Default = new IngredientGroupConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
CheckIngredientOrModifierViewModel vm = value as CheckIngredientOrModifierViewModel;
if (vm != null)
{
if (vm.ItemModifiers.Any())
{
StringBuilder builder = new StringBuilder(vm.LowercaseCheckTitle);
foreach (var mod in vm.ItemModifiers)
{
builder.Append("|");
builder.Append(mod.Title);
}
return builder.ToString();
}
else
{
return vm.LowercaseCheckTitle;
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
答案 0 :(得分:0)
我弄明白了这个问题。这是一个布局问题;我向ItemsControl添加了Top-Margin 40,它显示了缺少的第一项。