WPF转换器异常行为

WPF转换器异常行为,wpf,binding,converter,actualwidth,Wpf,Binding,Converter,Actualwidth,我将文本框的宽度绑定到其包含网格的实际宽度,如下所示: <TextBlock Grid.Column="0" Text="WorkPortalView UserControl" TextTrimming="CharacterEllipsis" Foreground="White" Margin="5" Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncest

我将
文本框的
宽度
绑定到其包含
网格的
实际宽度
,如下所示:

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

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

显然,网格需要更多的空间来容纳文本块。您可能可以在d-87D处找到这种行为的截止点,因为网格似乎需要在每个文本块宽度更改上额外增加12像素


顺便说一下,如果您只想给textblock一个相对大小,那么最好将其用于网格列

显然,网格需要更多的空间来容纳文本块。您可能可以在d-87D处找到这种行为的截止点,因为网格似乎需要在每个文本块宽度更改上额外增加12像素


顺便说一下,如果您只想给textblock一个相对大小,那么最好将其用于网格列

这似乎是一个bug。这种行为对于一次“自动调整”迭代是合理的,但不应导致无限循环。请参见上面的讨论


最后我切换到了
DockPanel
,以实现所需的布局。

这似乎是一个bug。这种行为对于一次“自动调整”迭代是合理的,但不应导致无限循环。请参见上面的讨论


最后,我切换到
DockPanel
以获得所需的布局。

是否使用TextBlock resize调整网格大小?@Wonko:不,但它似乎与另一列(两列上的宽度=自动)的交互有关,当我没有为第二列留出足够的宽度时,会触发该列;将方程设置为d-100D(为另一列留出更多空间)似乎可以解决这个问题。我仍然不知道触发条件,以便能够知道什么是“足够”的空间来容纳另一列,为什么它不只是切断另一列,或者做一些其他的最终决定。网格大小是否随TextBlock resize一起调整?@Wonko:不,但它似乎与与另一列的交互有关(两列上的宽度=自动)当我没有为第二列留出足够的宽度时触发;将等式设置为d-100D(为另一列留出更多空间)似乎可以纠正问题。我仍然不知道触发条件,无法知道什么是“足够”另一列的空间,为什么它不只是切断另一列,或者做一些其他的最终决定。谢谢Eren.+1的好建议。我想我的问题的答案是这是一个bug。我想答案是我一定是因为没有给第二列足够的空间而触发了网格大小的变化。不过,我还是会ink对网格大小进行一次“自动”调整应能纠正布局,布局应解析为稳定的调整设置,而不是导致无限循环。因此,我认为这可能是一个缺陷。@Bill这不是一个缺陷。请注意,“自动”的意思是“给我一个刚好适合我的内容的大小”。它根据内容而不是可用空间来确定大小。如果它的内容需要100 px,则与将宽度设置为100 px的固定大小相同。在您的情况下,第二列内容的大小不会改变;因此,“自动”调整大小的列不会改变其大小。我不清楚您对自动调整行为的描述如何与我所描述的转换器的行为相关联。@比尔,我的意思是,由于自动调整大小的列具有固定的大小。因此,由于网格的大小是两列的总和,因此当当第一列的大小增加时(因为第二列的大小是固定的)。网格大小的变化会触发列大小的另一个变化,因为绑定会触发网格大小的变化,等等。希望我说得有道理:)谢谢Eren+1.寻求好的建议。我想我的问题的答案是这是一个bug。我想答案是,我一定是因为没有给第二列足够的空间而触发了网格大小的变化。尽管如此,我还是认为对网格大小进行一次“自动”调整应该可以纠正布局,并且布局应该解析为稳定的调整设置,而不是导致无限循环。因此,我认为这可以说是一个错误。@Bill这不是一个错误。请注意,“自动”的意思是“给我一个大小刚好适合我的内容”。它根据内容而不是可用空间确定大小。如果它的内容需要100px,这与将宽度设置为100px的固定大小是一样的。在您的情况下,第二列内容的大小没有改变;因此,“自动”大小的列不会改变其大小。我不清楚您对自动行为的描述如何与我所描述的转换器的行为相联系。@比尔,我的意思是,由于这种行为,您可以假定自动大小列的大小是固定的。因此,由于网格的大小是两列的总和,因此每当第一列的大小增加时,网格的大小就必须增加(因为第二列的大小是固定的)。网格大小的更改会触发列大小的另一个更改,因为绑定会触发网格大小的更改,依此类推。希望我说的有道理:)