2012-01-18 5 views
0

StackPanelの中にTextBlockがあります。 TextTrimmingを使用しているので、StackPanel.ActualWidthに従ってTextBoxの幅を設定する必要があります。バインディング対マルチバインディング=>異なる結果

<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> 

マイコンバータ:

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 

なぜこれが正しくありませんそのコード以下の作業? (同じコードでIValueConverterを使用して):

私のコンバータはStackPanelが、ActualWidth常にゼロ

<TextBlock.Width> 

    <Binding RelativeSource="{x:Static RelativeSource.Self}" Path="TemplatedParent.Parent.ActualWidth" Converter="{StaticResource WidthConverter}" />   
</TextBlock.Width> 
+0

を試す得ることができますシングル)BEFORE TextBlokレンダリング – Avlin

+0

こんにちは、私は画像ソースへのバインディングで、同じ問題を抱えている可能性があります。私はそれについての話題とテストする小さなプロジェクトを投稿しました:http://stackoverflow.com/questions/10128307/special-behavior-of-multibinding-vs-binding-when-specifying-an-imagesource –

答えて

0

ですが、私はMultiBindingのはTextBlockのレンダリング後に行われることを考えると、(

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