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="&#xf142;"
 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="&#xf0c4;"
 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();
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值