在wpf中动态添加控件 [英] Dynamically Adding Controls in wpf

查看:458
本文介绍了在wpf中动态添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个arraylist。我将这些arraylist元素动态添加到标签中。我没有得到正确的对齐。让我知道我哪里错了。我的输出应该是这样的。



Crosin 10 20删除按钮



Saridon 20 30删除按钮



PainKiller 30 40删除按钮







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.

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;
       }
       
       
   }
}

推荐答案

嗨Subbarayudu,



请尝试以下代码:



MainWindow.cs:



Hi Subbarayudu,

Try the following code:

MainWindow.cs:

<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; }
}


这篇关于在wpf中动态添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆