WP中MultiBinding的用法

在一个使用场景中,我需要根据两个条件去控制border控件最下面的边是否显示,于是就有了下面的用法。
前端调用如下:

<DataTemplate>
 <Border BorderBrush="#DBDDDF" Tag="{Binding}">
 <!--集合最后的项不显示-->
 <Border.BorderThickness>
 <MultiBinding Converter="{StaticResource TestBorderMultibindingConverter}">
 <Binding RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}" Path="ItemsSource"/>
 <Binding RelativeSource="{RelativeSource Mode=Self}" Path="DataContext"/>
 </MultiBinding>
 </Border.BorderThickness>
 <Grid Background="{Binding Path=.,Converter={StaticResource TestProgressBackgroundConverter}}">
 <TextBox Text="{Binding Path=.,Converter={StaticResource TestProgressContentConverter}}" 
 Style="{StaticResource GeneralTextBoxStyle}" IsReadOnly="True"/>
 <!--上下文菜单-->
 <Button x:Name="more" Content=""
 Tag="{Binding Path=.}"
 Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid,Mode=FindAncestor},Path=DataContext.OpenFolder23MenuCommand}"
 CommandParameter="{Binding ElementName=popup}"
 Style="{StaticResource OpenFolder23ButtonStyle}"/>
 <Popup x:Name="popup" Tag="{Binding Path=.}"  StaysOpen="False"  PopupAnimation="Slide" AllowsTransparency="True" PlacementTarget="{Binding  ElementName=more}" Placement="Relative">
 <Border Background="White" BorderBrush="#C4C4C4" BorderThickness="1" CornerRadius="5">
 <StackPanel>
 <Button Content="删除项" Tag=""
 Style="{StaticResource ContextMenuButtonStyle}"
 Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid,Mode=FindAncestor},Path=DataContext.DeleteReportCommand}"
 CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Popup},Path=DataContext}"/>
 </StackPanel>
 </Border>
 </Popup>
 </Grid>
 </Border>
 <DataTemplate.Triggers>
 <Trigger Property="IsMouseOver" Value="True">
 <Setter Property="Visibility" Value="Visible" TargetName="more"/>

 </Trigger>
 <Trigger Property="IsMouseOver" Value="False">
 <Setter Property="Visibility" Value="Hidden" TargetName="more"/>
 </Trigger>
 </DataTemplate.Triggers>
 </DataTemplate>

主要用了一个多值转换器TestBorderMultibindingConverter去控制border。
注意多值绑定的写法:

转换器如下

public class TestBorderMultibindingConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Thickness thickness =new Thickness(0, 0, 0, 1);
            var list = values[0] as ICollection<Report>;
            Report model = values[1] as Report;
            if (list != null && model != null)
            {
                var index = list.ToList().IndexOf(model);
                if (index == list.Count - 1)
                {
                    thickness = new Thickness(0, 0, 0, 0);//集合最后一个元素没有下划线
                }
            }
            return thickness;
        }

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

你可能感兴趣的:(WPF,wpf,multibinding)