0

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.

4

2 回答 2

2

RowDefinition 不是 FrameworkElement,因此它没有 DataContext。绑定在这里不起作用。不过,您可以在该行中放置一个 Rectangle,将行高设置为 Auto 并将 Rectangle.Height 绑定到您想要的属性。这可能适用于某些情况。对于其他人来说,可能有更好的解决方案,但您需要具体说明您想要实现的目标。

于 2013-07-08T06:19:01.483 回答
0

因此,似乎属性类型投影没有像您或我期望的那样工作。它将属性包装GridLength在一个IReference<T>(或者,在 C# 中,Nullable<T>)中。尝试绑定到MyHeight.Value.

于 2013-07-05T23:53:03.673 回答