2012-05-10 136 views
1

我绑定的TextBoxWidth其含有GridActualWidth,如下所示:WPF转换器奇怪的行为

<TextBlock 
    Grid.Column="0" 
    Text="WorkPortalView UserControl" 
    TextTrimming="CharacterEllipsis" 
    Foreground="White" 
    Margin="5" 
    Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Converter={StaticResource textWidthConverter}, diag:PresentationTraceSources.TraceLevel=High}" /> 

如图所示,我使用的转换器,如下所示:

class TextWidthConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      double? d = value as double?; 
      if (d == null) 
       return null; 

      return (d/2); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotSupportedException("Cannot convert back"); 
     } 
    } 

奇怪的是,显示的代码工作正常。但是,如果我用return (d - 75D)替换return (d/2)声明,它似乎引发了无限循环的属性更改。

这里是从d诊断跟踪标签输出/ 2:

System.Windows.Data Warning: 71 :  Lookup ancestor of type Grid: queried Grid (hash=19487651) 
System.Windows.Data Warning: 70 : RelativeSource.FindAncestor found Grid (hash=19487651) 
System.Windows.Data Warning: 76 : BindingExpression (hash=35097890): Activate with root item Grid (hash=19487651) 
System.Windows.Data Warning: 106 : BindingExpression (hash=35097890): At level 0 - for Grid.ActualWidth found accessor DependencyProperty(ActualWidth) 
System.Windows.Data Warning: 102 : BindingExpression (hash=35097890): Replace item at level 0 with Grid (hash=19487651), using accessor DependencyProperty(ActualWidth) 
System.Windows.Data Warning: 99 : BindingExpression (hash=35097890): GetValue at level 0 from Grid (hash=19487651) using DependencyProperty(ActualWidth): '417.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=35097890): TransferValue - got raw value '417.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=35097890): TransferValue - user's converter produced '208.8' 
System.Windows.Data Warning: 87 : BindingExpression (hash=35097890): TransferValue - using final value '208.8' 

下面是从诊断跟踪标签的输出d - 75D:

System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '429.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '429.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '354.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '354.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '441.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '441.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '366.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '366.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '453.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '453.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '378.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '378.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '465.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '465.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '390.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '390.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '477.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '477.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '402.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '402.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '489.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '489.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '414.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '414.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '501.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '501.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '426.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '426.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '513.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '513.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '438.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '438.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '525.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '525.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '450.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '450.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '537.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '537.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '462.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '462.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '549.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '549.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '474.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '474.6' 
System.Windows.Data Warning: 94 : BindingExpression (hash=58235710): Got PropertyChanged event from Grid (hash=43090134) for ActualWidth 
System.Windows.Data Warning: 99 : BindingExpression (hash=58235710): GetValue at level 0 from Grid (hash=43090134) using DependencyProperty(ActualWidth): '561.6' 
System.Windows.Data Warning: 78 : BindingExpression (hash=58235710): TransferValue - got raw value '561.6' 
System.Windows.Data Warning: 80 : BindingExpression (hash=58235710): TransferValue - user's converter produced '486.6' 
System.Windows.Data Warning: 87 : BindingExpression (hash=58235710): TransferValue - using final value '486.6' 

什么有什么想法解释了转换器的这种行为?

+0

网格调整大小与TextBlock调整? –

+0

@Wonko:不,但它似乎与另一列(两列上的宽度=自动)的交互有关,当我没有为第二列留足够的宽度时会触发它;将公式设置为d - 100D(为其他列留出更多空间)似乎可以纠正问题。我仍然不知道触发条件能够知道另一列的“足够”空间,为什么它不会截断另一列,或做出其他最终决定。 – Bill

回答

1

显然,网格需要更多的文本块空间。您可能能够在d-87D中找到此行为的截止点,因为网格似乎每个文本块宽度更改需要额外的12像素。

顺便说一下,如果您只想给textblock一个相对大小,最好使用star sizing作为网格列。

+0

谢谢Eren。 +1为好建议。我认为我的问题的答案是这是一个错误。我认为答案是,我必须通过不允许第二列足够空间而触发网格大小更改。不过,我认为对网格大小进行一次“自动”调整应该改正布局,并且布局应该解析为稳定的调整设置,而不会导致无限循环。因此,这可以说是我认为的一个错误。 – Bill

+0

@Bill这不是一个错误。请注意,“自动”意思是“给我一个尺寸足以适合我的内容”。它根据其内容确定大小,而不是基于可用空间。如果结果显示它的内容需要100 px,则将宽度设置为100 px的固定大小。在你的情况下,第二列内容的大小不会改变;因此,“自动”大小的列不会更改其大小。 –

+0

我不清楚你对汽车行为的描述如何描述我所描述的转换器的行为。 – Bill

0

这似乎是一个错误。一个“自动调整”迭代的行为是合理的,但不应该导致无限循环。请参阅上面的讨论。

我最终切换到DockPanel以实现所需的布局。