关于wpf中binding如何给converter传递控件

<Popup x:Name="dispatchPop"
                   PlacementTarget="{Binding ElementName=griddatalist}"
                   StaysOpen="True"
                   IsOpen="False"
                   Width="{Binding ElementName=griddatalist, Path=ActualWidth, Converter={StaticResource actualWidth}}" Height="20"
                   Placement="Left"
                   Placement="Bottom"
                   PopupAnimation="Fade"
                   AllowsTransparency = "True"
                   x:Name="dispatchPop"
                   HorizontalOffset="{Binding ElementName=griddatalist, Path=ActualWidth,UpdateSourceTrigger=PropertyChanged}"
                   >


                <Popup.VerticalOffset>
                    <MultiBinding Converter="{StaticResource ver}">
                        <Binding ElementName="griddatalist,Path=DataContext,UpdateSourceTrigger=PropertyChanged"/>
                        <Binding ElementName="griddatalist" Path="ActualHeight" UpdateSourceTrigger="PropertyChanged"/>
                        <Binding ElementName="griddatalist" Path="ActualWidth" UpdateSourceTrigger="PropertyChanged"/>
                    </MultiBinding>
                </Popup.VerticalOffset>

                   VerticalOffset="-15"  >

                <Border Width="{Binding ElementName=griddatalist, Path=ActualWidth}" Height="20" Background="{StaticResource SelectedLine}">
                    <TextBlock x:Name="selectedCount" Height="20" Width="55" Margin="8,2,8,2" FontSize="12" Foreground="#FFFFFF"/>
                    <TextBlock x:Name="selectedCount" Height="20" Width="55" Margin="8,2,8,2" FontWeight="Bold" FontSize="12" Foreground="#FFFFFF"/>
                </Border>
            </Popup>

主要是这句话

<MultiBinding Converter="{StaticResource ver}">
                        <Binding ElementName="griddatalist"/>#这里绑定了控件
                        <Binding ElementName="griddatalist" Path="ActualHeight" UpdateSourceTrigger="PropertyChanged"/>
                        <Binding ElementName="griddatalist" Path="ActualWidth" UpdateSourceTrigger="PropertyChanged"/>
                    </MultiBinding>

下面是converter

public class DispatchListPopupVer : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {

            DataGrid grid = values[0] as DataGrid;

            double horizon = 0;

            double.TryParse(values[1].ToString(), out horizon);

            ScrollViewer scrollview = FindVisualChild<ScrollViewer>(grid);

            if (scrollview != null)
            {
                if (scrollview.ComputedHorizontalScrollBarVisibility== Visibility.Visible)
                {
                    return horizon - 34;
                }
                else
                {
                    return horizon - 20;
                }
            }
            else
            {
                 return horizon - 34;
            }

        }

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

        private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF绑定转换器(Binding Converter)是一种用于在数据绑定过程进行数据转换的功能。在WPF,我们可以使用数据绑定来将数据源(如属性、集合等)与UI元素(如控件属性、集合等)进行关联。而绑定转换器则可以在将数据源的数据传递给UI元素之前,对数据进行一些转换操作。 绑定转换器通常实现了IValueConverter接口,该接口定义了两个方法:Convert和ConvertBack。Convert方法用于将数据源的值转换为目标类型的值,而ConvertBack方法则用于将目标类型的值转换回数据源类型的值。在进行数据绑定时,我们可以通过指定绑定转换器来对数据进行适当的转换。 使用绑定转换器有助于实现更灵活的数据绑定操作。例如,我们可以使用绑定转换器将数据源的布尔值转换为可见性类型,以便控制UI元素的可见性。或者,我们可以使用绑定转换器将数据源的时间戳转换为可读的日期时间格式。还可以使用绑定转换器将数据源的枚举值转换为对应的文本描述。 要使用绑定转换器,我们首先需要实现一个继承自IValueConverter接口的类。然后,在XAML定义该绑定转换器,并在相应的数据绑定指定使用该转换器。可以通过绑定转换器的Converter属性来实现。 总而言之,WPF绑定转换器是一种在数据绑定过程进行数据转换的机制。它允许我们灵活地对数据进行适当的转换,以满足UI元素与数据源之间的差异。通过使用绑定转换器,我们可以更加简单而且高效地处理数据绑定相关的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值