我将Width
a 的 a绑定TextBox
到ActualWidth
其包含的Grid
,如下所示:
<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}" />
如图,我使用的是Converter,如下:
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 / 2)
语句替换为return (d - 75D)
,似乎会引发属性更改的无限循环。
这是 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'
关于什么解释了转换器的这种行为的任何想法?