使用AlternationIndex格式化/样式

时间:2015-02-12 10:36:10

标签: wpf xaml

我有一个itemscontrol,我想显示一个按钮,如果它的mt最后一项。是否可以使用AlternationIndex执行此操作?

<Button.Style>
  <Style TargetType="{x:Type Button}" BasedOn="{StaticResource RoundButton}">
    <Style.Triggers>
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="0"> <!-- What do I give here? 0 hides first row -->
        <Setter Property="Visibility" Value="Collapsed"/>
      </DataTrigger>
    </Style.Triggers>
  </Style>
</Button.Style>

1 个答案:

答案 0 :(得分:1)

也许有点复杂,但您可以在DataTrigger中使用MultiBinding,将AlternationIndex属性与ItemsControl项目的Count属性进行比较:

<Style TargetType="Button" BasedOn="{StaticResource RoundButton}">
    <Setter Property="Visibility" Value="Collapsed"/>
    <Style.Triggers>
        <DataTrigger Value="True">
            <DataTrigger.Binding>
                <MultiBinding Converter="{StaticResource IsLastItemConverter}">
                    <Binding Path="(ItemsControl.AlternationIndex)"
                             RelativeSource="{RelativeSource
                                              AncestorType=ContentPresenter}"/>
                    <Binding Path="Items.Count"
                             RelativeSource="{RelativeSource
                                              AncestorType=ItemsControl}"/>
                </MultiBinding>
            </DataTrigger.Binding>
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

多值转换器如下所示:

public class IsLastItemConverter : IMultiValueConverter
{
    public object Convert(
        object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var index = (int)values[0];
        var count = (int)values[1];
        return index == count - 1;
    }

    public object[] ConvertBack(
        object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}