1

Width次のように、 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}" />

示されているように、次のようにコンバーターを使用しています。

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'

コンバーターのこの動作を説明するものについて何か考えはありますか?

4

2 に答える 2

1

どうやら、グリッドにはテキストブロック用により多くのスペースが必要です。d-87D でこの動作のカットオフ ポイントを見つけることができるかもしれません。これは、テキスト ブロックの幅を変更するたびにグリッドに 12 ピクセルを追加する必要があるように思われるためです。

ところで、テキストブロックに相対的なサイズを指定することだけが必要な場合は、グリッド列にスター サイズを使用することをお勧めします。

于 2012-05-10T18:26:01.637 に答える
0

これはバグのようです。この動作は、1 回の「自動調整」の反復では妥当ですが、無限ループになることはありません。上記の議論を参照してください。

DockPanel目的のレイアウトを実現するために、に切り替えました。

于 2012-05-10T18:44:41.360 に答える