Page 1 of 1

[Unity] Nested Data Bindings

Posted: 24 Sep 2014, 01:39
by MrHayato
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?

Re: [Unity] Nested Data Bindings

Posted: 24 Sep 2014, 02:21
by MrHayato
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>

Re: [Unity] Nested Data Bindings

Posted: 25 Sep 2014, 21:23
by sfernandez
It is a bug, please create a ticket in our bugtracker and we will try to fix it for the 1.1.12 version.