0

我对 Width 属性绑定有疑问。其中一排网格必须具有可变宽度。我决定使用绑定宽度属性来做到这一点,但这不起作用:

    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结构。注意,“*”不是整数。

第二:Width不是依赖属性(也没有实现INotifyPropertyChanged),所以你不能绑定到这个属性。

对于您的情况,我可以建议两种解决方案。尝试使用VisualStateManager或在其他属性的属性更改事件处理程序中实现您的逻辑。

编辑:或者您可以按照 Rana 的建议进行操作。将ColumnDefinition 属性的Width设置为“Auto”,并绑定到位于此单元格中的子项的 Width 属性。在这种情况下_avatarWidth应该double

于 2012-08-12T09:12:09.340 回答
1

只需使用 uielement.width 并在需要的地方指定宽度。我不知道您是如何实现代码的,但我想这将是您代码的单行版本。

于 2012-08-12T09:31:28.800 回答