PrimuS
PrimuS

Reputation: 2683

Access WPF object by string variable

I have x ListViews in my xaml Form:

In my program I can access them directly with lvExt1.Items or similar.

I would like to access those within a function that has the number as parameter

Something like:

privat void accessListView(string number){

   return lvlExt"number".Items;
}

In Symfony (PHP) I can do it like this: https://stackoverflow.com/a/31142123/1092632

I do realize it may be something completely different, but only to express what I am trying to do.

Upvotes: 0

Views: 1148

Answers (1)

Newton Zou
Newton Zou

Reputation: 588

Two ways:

  1. Use the way that WPF provided:

    var lvlExt = this.FindName("lvlExt" + number) as ListView;

    var lvlExt = LogicalTreeHelper.FindLogicalNode(this, "lvlExt" + number);

  2. Use an array to reference:

    var lvlExts = new ListView[3];

    ...

    var lvlExt =ListView[number];

Upvotes: 1

Related Questions