سؤال

I am trying to bind to a GridLength instance in a Windows Store app (compiled with the Visual Studio 2013 compiler) in C++/CX, but for some reason I keep getting the following error at runtime:

Error: Converter failed to convert value of type 'Windows.Foundation.IReference`1<Windows.UI.Xaml.GridLength>' to type 'GridLength'; BindingExpression: Path='MyHeight' DataItem='MyNamespace.MyObject'; target element is 'Windows.UI.Xaml.Controls.RowDefinition' (Name='null'); target property is 'Height' (type 'GridLength').

My code essentially looks like:

namespace MyNamespace
{
    [Windows::UI::Xaml::Data::Bindable]
    public ref class MyObject sealed
    {
        property Windows::UI::Xaml::GridLength MyHeight
        {
            Windows::UI::Xaml::GridLength get() { return myHeight; }
        }
    }
}

and my XAML file essentially looks like:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding MyHeight}"/>
    </Grid.RowDefinitions>
</Grid>

Just FYI, other properties in my class are binding properly. The issue appears to only be with the GridLength value struct. It is obviously finding the property correctly, but for some reason it can't match up the types.

هل كانت مفيدة؟

المحلول

A RowDefinition is not a FrameworkElement, so it doesn't have a DataContext. Binding won't work here. You can put a Rectangle in that row though, set the row height to Auto and bind the Rectangle.Height to a property you want. That could work for some scenarios. For others there might be better solutions, but you would need to specify what exactly you are trying to achieve.

نصائح أخرى

So, it appears that the property type projection isn't working as you or I would expect it to. It's wrapping the GridLength property in an IReference<T> (or, in C#, Nullable<T>). Try binding to MyHeight.Value.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top