MrHayato
Topic Author
Posts: 36
Joined: 19 Sep 2014, 21:29

[Unity] Nested Data Bindings

24 Sep 2014, 01:39

Hi,

I am trying to bind user controls nested within user controls, and it does not seem to be working.

Example:

DetailCardView.xaml
<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vs="clr-namespace:Assets.UI.VirtualShelf"
    xmlns:vm="clr-namespace:Assets.UI.ViewModels.VirtualShelf"
    x:Name="Root"
    x:Class="Assets.UI.VirtualShelf.DetailCardView"
    Margin="0,0,0,0">
  
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Category}"></TextBlock>
            <TextBlock Text="{Binding AtAGlance.Name}"></TextBlock>
            <vs:AtAGlanceView
                DataContext="{Binding AtAGlance}"
                Width="{Binding ActualWidth, ElementName=Root, Mode=OneWay}"
                Height="{Binding ActualHeight, ElementName=Root, Mode=OneWay}" />
        </StackPanel
    </Grid>
</UserControl>
AtAGlanceView.xaml
<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Assets.UI.VirtualShelf.AtAGlanceView">
    
    <Grid Background="White">
        <TextBlock Text="{Binding Name}" />
    </Grid>
</UserControl>
Test.cs
public class DetailCardViewTestComponent : MonoBehaviour
{
    public void Start()
    {
        var detailCard = GetComponent<NoesisGUIPanel>().GetRoot<DetailCardView>();
        var vm = new DetailCardViewModel
        {
            Category = "Foo",
            AtAGlance = new AtAGlanceViewModel
            {
                Name = "Bar"
            }
        };
    }
}
So this works fine in DetailCardView, and the bindings work for the textblocks there, but does not work for the AtAGlance control. Is there something I'm missing here?
 
MrHayato
Topic Author
Posts: 36
Joined: 19 Sep 2014, 21:29

Re: [Unity] Nested Data Bindings

24 Sep 2014, 02:21

Managed to get this working by using a ContentControl instead of setting DataContext on the custom control directly:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="AtAGlanceTemplate">
            <vs:AtAGlanceView
                DataContext="{Binding}"
                Width="{Binding ActualWidth, ElementName=Root, Mode=OneWay}"
                Height="{Binding ActualHeight, ElementName=Root, Mode=OneWay}" />
        </DataTemplate>
    </Grid.Resources>
    <StackPanel>
        <TextBlock Text="{Binding Category}"></TextBlock>
        <TextBlock Text="{Binding AtAGlance.Name}"></TextBlock>
        <ContentControl Content="{Binding AtAGlance}" ContentTemplate="{StaticResource AtAGlanceTemplate}" />
    </StackPanel
</Grid>
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: [Unity] Nested Data Bindings

25 Sep 2014, 21:23

It is a bug, please create a ticket in our bugtracker and we will try to fix it for the 1.1.12 version.

Who is online

Users browsing this forum: No registered users and 1 guest