本文介绍了结合VS MultiBinding =>不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的TextBlock 的StackPanel 。由于我使用 TextTrimming ,我必须设置manully根据文本框的宽度> StackPanel.ActualWidth 。

 < StackPanel中的Horizo​​ntalAlignment =拉伸>
    < TextBlock中的Horizo​​ntalAlignment =左>
        < TextBlock.Width>
            < MultiBinding转换器={StaticResource的WidthConverter}>
                < MultiBinding.Bindings>
                    <绑定的RelativeSource ={自我的RelativeSource}/>
                    <绑定的RelativeSource ={X:静态RelativeSource.Self}路径=TemplatedParent.Parent.ActualWidth/>
              < /MultiBinding.Bindings>
             < / MultiBinding>
        < /TextBlock.Width>
    < / TextBlock的>

我的转换器:

公共类WidthConverter
    实现IMultiValueConverter    公共职能转换(BYVAL值()为对象,BYVAL TARGETTYPE作为System.Type的,BYVAL参数作为对象,BYVAL文化System.Globalization.CultureInfo)作为对象实现System.Windows.Data.IMultiValueConverter.Convert        常量TextBoxMarginRight为双= 5        昏暗上级宽度为双= CTYPE(CTYPE(值(0),FrameworkElement的).Parent,FrameworkElement的).ActualWidth        昏暗ParentRelativeControlPosition作为点= CTYPE(值(0),FrameworkElement的).TransformToAncestor(((值(0),FrameworkElement的).Parent,Media.Visual)CTYPE CTYPE)变换(新点(0,0))        昏暗宽度为双=上级宽度 - TextBoxMarginRight - ParentRelativeControlPosition.X        如果宽度GT; 5然后
            返回宽度
        其他
            返回0
        万一    结束功能

这是为什么正确,工作不低于code? (使用的IValueConverter 与同一code):

我的转换器可以让的StackPanel ,但 ActualWidth的始终为零

 < TextBlock.Width>    <绑定的RelativeSource ={X:静态RelativeSource.Self}路径=TemplatedParent.Parent.ActualWidth转换器={StaticResource的WidthConverter}/>
< /TextBlock.Width>


解决方案

尝试

  WIDTH ={绑定路径= ActualWidth的,
                的RelativeSource = {的RelativeSource FindAncestor,AncestorType =的StackPanel}}

I have a TextBlock inside a StackPanel. Since I'm using TextTrimming, I have to set manully the TextBox's width according the StackPanel.ActualWidth.

<StackPanel HorizontalAlignment="Stretch">
    <TextBlock HorizontalAlignment="Left">
        <TextBlock.Width>
            <MultiBinding Converter="{StaticResource WidthConverter}">
                <MultiBinding.Bindings>
                    <Binding RelativeSource="{RelativeSource Self}" />
                    <Binding RelativeSource="{x:Static RelativeSource.Self}" Path="TemplatedParent.Parent.ActualWidth"  />
              </MultiBinding.Bindings>
             </MultiBinding>                
        </TextBlock.Width>
    </TextBlock>

My converter:

Public Class WidthConverter
    Implements IMultiValueConverter

    Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert

        Const TextBoxMarginRight As Double = 5

        Dim ParentWidth As Double = CType(CType(values(0), FrameworkElement).Parent, FrameworkElement).ActualWidth

        Dim ParentRelativeControlPosition As Point = CType(values(0), FrameworkElement).TransformToAncestor(CType(CType(values(0), FrameworkElement).Parent, Media.Visual)).Transform(New Point(0, 0))

        Dim Width As Double = ParentWidth - TextBoxMarginRight - ParentRelativeControlPosition.X

        If Width > 5 Then
            Return Width
        Else
            Return 0
        End If

    End Function

Why is this working correctly and not that code below ? (using a IValueConverter with same code):

My converter can get the StackPanel but ActualWidth is always zero

<TextBlock.Width>

    <Binding RelativeSource="{x:Static RelativeSource.Self}" Path="TemplatedParent.Parent.ActualWidth"  Converter="{StaticResource WidthConverter}" />          
</TextBlock.Width>
解决方案

try

Width="{Binding Path=ActualWidth, 
                RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel}}"

这篇关于结合VS MultiBinding =&GT;不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 00:22