This project is read-only.

Custom Object

Jan 5, 2015 at 4:23 PM
Hello everyone,

It's nice to have such a powerful tool in your hands, however I have been struggling to make a custom object to show in ElementFlow. I did made my own class which holds two strings.
namespace WpfApplication1
{
    public class Info
    {
        public string slika;
        public string text;

        public Info(string x, string y)
        {
            slika = y;
            text = x;
        }
    }
}
And then in StringCollection class i put my class as type
public class StringCollection : ObservableCollection<Info>
Then I changed the DataTemplate
    <Window.Resources>

        <WpfApplication1:StringCollection x:Key="DataSource" />
        
        <DataTemplate x:Key="TestDataTemplate" 
                      DataType="{x:Type WpfApplication1:Info}">
       
            <Border x:Name="ElementVisual"
                    Background="Black"
                    Padding="5"
                    BorderThickness="5"
                    BorderBrush="LightGray"
                    Grid.Row="10">
                <Grid>
                    <Image Source="{Binding Path=slika}" 
                           Stretch="Fill" />
                    <Label Content="{Binding text}" Foreground="#FFFF0E0E" />
                </Grid>
            </Border>

        </DataTemplate>

    </Window.Resources>
I am populating the DataSource with this code
        private StringCollection _dataSource;
        private Info j;

        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            _dataSource = FindResource("DataSource") as StringCollection;
            
            
            for (int i = 0; i <= 10; i++)
            {
                 j = new Info("X1.png", "X1.png");
                _dataSource.Insert(i, j);
            }
            _elementFlow.SelectedIndex = 7;
        }
The program is displaying the covers but they are empty. Can someone please clarify why this isn't working? Thanks in advance.
Jan 5, 2015 at 8:56 PM
Well I solved it, but for newcomers and beginners here is what I did. First You need to put getter and setters in Info class. So finally it will look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication1
{
    public class Info
    {
        private string slika;
        private string text;

        public string Slika
        {
            get 
            {
                return slika; 
            }
            set 
            {
                slika = value; 
            }
        }

        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        public Info(string x, string y)
        {
            slika = y;
            text = x;
        }
    }
}
After that You just call the property "Slika" or "Text" to bind the picture or label to one of them like this:
    <Window.Resources>

        <WpfApplication1:StringCollection x:Key="DataSource" />
        
        <DataTemplate x:Key="TestDataTemplate" 
                      DataType="{x:Type WpfApplication1:StringCollection}">
       
            <Border x:Name="ElementVisual"
                    Background="Black"
                    Padding="5"
                    BorderThickness="5"
                    BorderBrush="LightGray"
                    Grid.Row="1">
                <Grid>
                    <Image Stretch="Fill" Source="{Binding Slika}" />
                    <Label Content="{Binding Text}" Foreground="Black" VerticalAlignment="Bottom" HorizontalAlignment="Center" Width="10" Height="10"  />
                </Grid>
            </Border>

        </DataTemplate>

    </Window.Resources>