简体   繁体   中英

Using a User Control as a DataTemplate and Binding Properties

I've set up a User Control that displays a Title, Image, Collection of Players, and Time. This worked great until I stuck it into a GridView and tried to Bind these properties to the values of the elements.

Now it reports a number of errors, specifically, "The member 'X' is not recognized or not accessible". This occurs in my bindings here:

<GridView.ItemTemplate>
    <DataTemplate>
        <local:GamePane Map="{Binding Map}" Players="{Binding Players}"/>
    </DataTemplate>
</GridView.ItemTemplate>

I am using DependencyProperties to try to get everything to work, like this:

    public string Map
    {
        get { return (string)GetValue(MapProperty); }
        set
        {
            SetValue(MapProperty, value);
        }
    }

    public static readonly DependencyProperty MapProperty =
    DependencyProperty.Register(
        "Map",                    
        typeof(string),           
        typeof(GamePane),         
        new PropertyMetadata(     
            "Unknown",            
            ChangeMap)
        );

This got my code compiling, however, now they just return the default value even after I have specified something different through my bindings. What am I doing wrong?

I'll answer this, as I have found what I was doing wrong.

In the User Control I set it's Data Context to itself. This was causing the issues I was having. Removing this Data Context fixed everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM