paevensonKratos
Topic Author
Posts: 9
Joined: 11 Mar 2014, 20:24

ComboBox in DataTemplate inside Unity

23 May 2014, 17:16

Here is the code:

XAML
DataTemplate
 <Grid.Resources>
        <DataTemplate x:Key="InteractionTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="20"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="100"/> 
					<ColumnDefinition Width="100"/> 
                </Grid.ColumnDefinitions>
							
                <TextBlock  Height="10" Text="{Binding Index}" Grid.Column="0" />
                <TextBlock Text="{Binding ObjectName}" Margin="5, 0, 5, 0" Grid.Column="1"/>
                <TextBlock Text="{Binding ObjectState}" Margin="5, 0, 5, 0" Grid.Column="2"/>
               <ComboBox x:Name="ModelTypeComboBox" Height="24" Width="79" Grid.Column="3">
                    <ComboBoxItem x:Name="ModelTypeBoxUpper" Content="Upper" IsSelected="True"/>
                    <ComboBoxItem x:Name="ModelTypeBoxLower" Content="Lower"/>
                    <ComboBoxItem x:Name="ModelTypeBoxBuccal" Content="Buccal"/>
                </ComboBox>					
            </Grid>
        </DataTemplate>
    </Grid.Resources>
ListBox
<ListBox Height="196" ItemsSource="{Binding ActionCollection, ElementName=Root}" ItemTemplate="{StaticResource InteractionTemplate}">
When i try to view it inside Unity the combo box is empty:

Image

This thread sounded like a similar problem....viewtopic.php?f=3&t=344

What am I doing wrong?
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: ComboBox in DataTemplate inside Unity

23 May 2014, 19:55

The ListBox.ItemsSource has a Binding that points to the ActionCollection property of the element named "Root".
Are you sure that "Root" element exposes the "ActionCollection" property?
Which type of element is "Root"?

Maybe you set a View Model in the Root element, and your binding should look just like this:
ItemsSource="{Binding ActionCollection}"
 
paevensonKratos
Topic Author
Posts: 9
Joined: 11 Mar 2014, 20:24

Re: ComboBox in DataTemplate inside Unity

23 May 2014, 20:37

My binding is working properly. Root is just pointing to the Noesis.UserControl. Iv'e tried this code as well.
Hi Rob,

Based on your xaml, I tried two options to assign a DataTemplate to an item: using the ListBoxItem.ContentTemplate and using the ListBox.ItemTemplate; both worked well.
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid.Resources>
        <DataTemplate x:Key="ModelLayerTemplate">
            <Grid HorizontalAlignment="Left" Height="60" Margin="0,0,0,0" VerticalAlignment="Top" Width="168">
                <Rectangle RadiusY="5" RadiusX="5" Fill="#BF6C87CC"/>
                <TextBox x:Name="ModelNameTextBox" Margin="32.5,5,0,0" TextWrapping="Wrap" Text="Model Name" FontSize="12" Background="#FF5368A1" Foreground="#FFDADADA" Height="25" VerticalAlignment="Top" Width="130" HorizontalAlignment="Left" BorderBrush="{x:Null}"/>
                <ComboBox x:Name="ModelTypeComboBox" Height="24" Margin="32,0,0,5" VerticalAlignment="Bottom" Width="79" HorizontalAlignment="Left">
                    <ComboBoxItem x:Name="ModelTypeBoxUpper" Content="Upper" IsSelected="True"/>
                    <ComboBoxItem x:Name="ModelTypeBoxLower" Content="Lower"/>
                    <ComboBoxItem x:Name="ModelTypeBoxBuccal" Content="Buccal"/>
                </ComboBox>
                <ToggleButton x:Name="ModelToggleVisibilityButton" Content="X" HorizontalAlignment="Left" Margin="3,0,0,0" Style="{DynamicResource ToggleVisibilityEye}" Width="25" IsChecked="True" VerticalAlignment="Center" Height="25"/>
            </Grid>
        </DataTemplate>
    </Grid.Resources>

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

        <ListBox>
            <ListBoxItem Content="Hello" ContentTemplate="{StaticResource ModelLayerTemplate}"/>
        </ListBox>

        <ListBox ItemTemplate="{StaticResource ModelLayerTemplate}" Margin="0,10,0,0">
            <TextBlock Text="Item1"/>
        </ListBox>

    </StackPanel>

</Grid>
ListBox_Template.png
Is this what you want to achieve?
It doesnt work inside of Unity. I can see the combo boxes just fine in Blend, fully populated, but in Unity they are still blank.
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: ComboBox in DataTemplate inside Unity

23 May 2014, 23:05

If you are using your own UserControl as source of the items collection, you can try the following:
<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="DataTemplateTest"
  x:Name="Root">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="InteractionTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="100"/> 
                        <ColumnDefinition Width="100"/> 
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Index}" Grid.Column="0" VerticalAlignment="Center"/>
                    <TextBlock Text="{Binding ObjectName}" Margin="5, 0, 5, 0" Grid.Column="1" VerticalAlignment="Center"/>
                    <TextBlock Text="{Binding ObjectState}" Margin="5, 0, 5, 0" Grid.Column="2" VerticalAlignment="Center"/>
                    <ComboBox x:Name="ModelTypeComboBox" Height="24" Grid.Column="3">
                        <ComboBoxItem x:Name="ModelTypeBoxUpper" Content="Upper" IsSelected="True"/>
                        <ComboBoxItem x:Name="ModelTypeBoxLower" Content="Lower"/>
                        <ComboBoxItem x:Name="ModelTypeBoxBuccal" Content="Buccal"/>
                    </ComboBox>               
                </Grid>
            </DataTemplate>
        </Grid.Resources>
        <ListBox Height="196" HorizontalAlignment="Center"
            ItemsSource="{Binding ActionCollection, ElementName=Root}"
            ItemTemplate="{StaticResource InteractionTemplate}"/>
    </Grid>
</UserControl>
[Noesis.Extended]
public class DataItem : Noesis.BaseComponent
{
    public uint Index { get; set; }
    public string ObjectName { get; set; }
    public string ObjectState { get; set; }
}

[Noesis.Extended]
[Noesis.UserControlSource("Assets/Test/_Test.xaml")]
public class DataTemplateTest : Noesis.UserControl
{
    public Noesis.Collection ActionCollection { get; private set; }

    public DataTemplateTest()
    {
        ActionCollection = new Noesis.Collection();
        ActionCollection.Add(new DataItem
        {
            Index = 0,
            ObjectName = "Jon",
            ObjectState = "Active"
        });
        ActionCollection.Add(new DataItem
        {
            Index = 1,
            ObjectName = "Pet",
            ObjectState = "Inctive"
        });
        ActionCollection.Add(new DataItem
        {
            Index = 2,
            ObjectName = "Ras",
            ObjectState = "Active"
        });
    }
}
This scenario is working for me.

Who is online

Users browsing this forum: No registered users and 3 guests