0

Width プロパティのバインドに問題があります。grid の行の 1 つは可変幅でなければなりません。バインディング幅プロパティでこれを行うことにしましたが、これは機能しません:

    private Int32 _avatarWidth;
    public Int32 AvatarWidth
    {
        get { return _avatarWidth; }
        set
        {
            _avatarWidth = value;
            RaisePropertyChanged(() => AvatarWidth);
        }
    }

XAML:

<ListBox Grid.Row="1" ItemsSource="{Binding CurrentDialog.Messages}" 
        HorizontalAlignment="Stretch" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            ......
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="{ Binding AvatarWidth }" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            .......
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

しかし、これはうまくいきません。デザイナーには例外があります:

System.Reflection.TargetInvocationException
Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)


System.InvalidOperationException
Layout measurement override of element 'Microsoft.Windows.Design.Platform.SilverlightViewProducer+SilverlightContentHost' should not return PositiveInfinity as its DesiredSize, even if Infinity is passed in as available size.
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Designer.DeviceSkinViewPresenter.DeviceDesignerBackground.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Decorator.MeasureOverride(Size constraint)
at Microsoft.Windows.Design.Interaction.DesignerView.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Designer.Viewport.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
at System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged)
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Control.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at MS.Internal.DeferredHwndSource.ProcessQueue(Object sender, EventArgs e)
4

2 に答える 2

2

最初: ColumnDefinition のは Int32 ではなく、GridLength構造です。「*」は整数ではないことに注意してください。

2 番目: Widthは依存関係プロパティではない (そしてINotifyPropertyChangedを実装していない) ため、このプロパティにバインドすることはできません。

あなたの場合、2つの解決策を提案できます。VisualStateManagerを使用するか、他のプロパティのプロパティ変更イベント ハンドラーにロジックを実装してみてください。

編集:または、Ranaが提案したようにすることもできます. ColumnDefinitionプロパティのWidthを "Auto" に設定し、このセルにある子の Width プロパティにバインドします。この場合_avatarWidthdouble

于 2012-08-12T09:12:09.340 に答える
1

uielement.widthを使用して、必要な場所で幅を指定するだけです。コードをどのように実装するかはわかりませんが、コードの1行のエディションになると思います。

于 2012-08-12T09:31:28.800 に答える