wp7宽度属性绑定

时间:2012-08-12 09:07:48

标签: c# windows-phone-7 data-binding

我遇到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)

2 个答案:

答案 0 :(得分:2)

首先:ColumnDefinition的Width不是Int32,它是GridLength结构。注意,“*”不是整数。

第二:Width不是依赖属性(并且没有实现INotifyPropertyChanged),因此您无法绑定到此属性。

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

编辑:或者你可以按照Rana的建议去做。将ColumnDefinition属性的Width设置为“Auto”并绑定到此单元格中的child的Width属性。在这种情况下,_avatarWidth应为double

答案 1 :(得分:1)

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