Click here to Skip to main content
15,912,072 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi,

I have a arraylist. I am adding these arraylist elements dynamically to the lables. I am not getting correct alignment for this. let me know where i am going to wrong. My output should be like this.

Crosin 10 20 Deletebutton

Saridon 20 30 Deletebutton

PainKiller 30 40 Deletebutton

etc.

C#
public void populateform(ArrayList list)
{
    int i = 1;
    int count = 0;
    int listcount =list.Count;
    int rowvalue = 1;
    int colvalue = 0;
    int r1 = 1;
    
   for (int j = 0; j < listcount; j++)
   {
       Label lbl = new Label();
       lbl.Name = "myLabel" + i;
       lbl.Height = 30;
       lbl.Width = 200;
       lbl.Margin.Left.Equals(150);
       lbl.Content = list[j].ToString();
       Grid.SetRow(lbl, rowvalue);
       Grid.SetColumn(lbl, ++colvalue);
       if (j== 3)
       {
           colvalue = 0;
           rowvalue = 2;
           Grid.SetRow(lbl, rowvalue);
           Grid.SetColumn(lbl, ++colvalue);
           cartgrid.Children.Add(lbl);
           cartgrid.RegisterName(lbl.Name, lbl);
       }
       else
       {
           cartgrid.Children.Add(lbl);
           cartgrid.RegisterName(lbl.Name, lbl);
       }
            i++;
            ++count;
       
       if (j % 3 == 0)
       {
           
           int c1 = 4;
           Button btndelete = new Button();
           btndelete.Content = "Delete";
           btndelete.Name = "myButton" + i;
           btndelete.Width = 120;
           btndelete.Height = 35;
           btndelete.Click += new RoutedEventHandler(btndelete_Click);
           Grid.SetRow(btndelete, r1);
           Grid.SetColumn(btndelete, c1);
           cartgrid.Children.Add(btndelete);
           btncount++;
           ++r1;
       }
       
       
   }
}
Posted
Updated 20-Apr-14 2:54am
v2
Comments
Sergey Alexandrovich Kryukov 27-Sep-13 2:17am    
Not a complete question. What is the problem?
And don't use ArrayList, this class is obsolete, use System.Collections.Genetic.List.
—SA

1 solution

Hi Subbarayudu,

Try the following code:

MainWindow.cs:

XML
<Grid>
    <ItemsControl ItemsSource="{Binding Items}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" MinWidth="320">
                    <TextBlock Width="130" Text="{Binding Name}"/>
                    <TextBlock Width="130" Text="{Binding Number1}" />
                    <TextBlock Width="130" Text="{Binding Number2}" />
                    <Button Width="130" Content="Delete" Command="{Binding ClickCommand}"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>


And code behind:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new ViewModle();
    }
}

public class ViewModle : INotifyPropertyChanged
{
    public ViewModle()
    {
        InitItems();   
    }

    protected void InitItems()
    {
        var list = new ObservableCollection<Data>();

        var data1 = new Data() { Name = "Crosin", Number1 = 10, Number2 = 20 };
        data1.ClickCommand = new MeCommand(() => this.HandleDeleteClick(data1));
        list.Add(data1);

        var data2 = new Data() { Name = "Saridon", Number1 = 20, Number2 = 30 };
        data2.ClickCommand = new MeCommand(() => this.HandleDeleteClick(data2));
        list.Add(data2);

        var data3 = new Data() { Name = "PainKiller", Number1 = 30, Number2 = 40 };
        data3.ClickCommand = new MeCommand(() => this.HandleDeleteClick(data3));
        list.Add(data3);

        Items = list;
    }

    protected void HandleDeleteClick(Data data)
    {
        this.Items.Remove(data);

        NotifyPropertyChanged("Items");
    }

    public IList Items
    {
        get;
        set;
    }

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

public class MeCommand : ICommand
{
    public delegate void ExecuteMethod();

    private ExecuteMethod meth;

    public MeCommand(ExecuteMethod exec)
    {
        meth = exec;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        meth();
    }
}

public class Data
{
    public string Name { get; set; }

    public int Number1 { get; set; }

    public int Number2 { get; set; }

    public ICommand ClickCommand { get; set; }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900