Get items from DataTemplate for ArtOfTest controls - c#

I have this kind of object in code:
ArtOfTest.WebAii.Controls.Xaml.Wpf.ListBox
Items of it are in ItemTemplate. I have collection of it:
var listBoxItems = repairComapanyHintsList.Find.AllByType<ListBoxItem>();
and I want to get control from that DataTemplate. How can I do it? That solution doesn't work for DataTemplate (for other situations it's ok):
var textBlock = listBoxItem.Find.ByName("Name");
How can I get it? I tried this solution too, but that controls (from ArtOfTest) doesn't DependencyObject:
How can I find WPF controls by name or type?
I want to select one element depend of that TextBox text value.

I solved it, but it is not very good solution. It works for me and maybe it will help somebody.
I do it like that:
var listBoxItem = repairCompanyList.Find.AllByType<ListBoxItem>().FirstOrDefault(r => r.Text == "Name");
Assert.IsNotNull(listBoxItem, "Lack of expected list box item");
listBoxItem.User.Click();
I checked

Related

C# Winforms - Form Control Databinding - How to find source element

Please bear with me if this question is stupid or has been asked before. I understand how to databind a form control to a datasource. I would like now to reverse and retrieve the data source element for a specific control.
Example for a textbox:
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.table1BindingSource, "ContactID", true));
How can I retrieve the result "ContactID" for textBox1?
If you have only one Binding added to the DataBindings of Textbox then you can get the datamember value of the binding as following.
var binding = textBox1.DataBindings[0];
var member = binding.BindingMemberInfo.BindingMember;
MessageBox.Show(member);
This should get you the value which are looking for.

How can i find item in wpf listbox having grouped list bind to it?

My listbox is having a grouped list so basically I want to find listbox group item index with item value. Listbox is having item source bind to it and is having DisplayMemberPath and SelectedValuePath set from code behind.
What I have tried yet are as follow:-
int index = istboxName.Items.IndexOf(ListBindToItemSource.particularParameterValue);
Give index=-1 always.
Another solution I tried is:
int index = ListboxName.Items.Groups.IndexOf(ListBindToItemSource.particularParameterValue);
Same result index=-1 always.
You should never need to access the items that way, instead access the item in your bound source and manipulate that. If you want to change anything in the view, like e.g. a Background, bind it on your item and change it at the source.

Getting SubItem from listview

I am trying to get Subitem from a list view, I did this but doesn't work:
curItem1=listView2->Items[i]->Text;
curItem2=listView2->SubItem[i]->Text;
ListViewItem selItem = listView1.Items[i];
string txt = selItem.SubItems[index].Text;
To get the Text is wrong in first line.
Try this... ListView1.SelectedItems(0).SubItems(i).Text
You want to access the Text property of a certain control inside the ListViews ItemTemplate. This is done similarly to this:
string curItem = ((TextBox)listView2.Items[i].FindControl("TextBox1")).Text;
Where 'TextBox1' is the control you are trying to get the Text property of. You also need to make sure that you are casting to the correct type of the Control you are trying to get the property for.

Sorting a ListBox ListItems with LINQ?

I'm trying to sort the items in a ListBox. However, upon doing so the Item Value gets set to the Item Text. Any help would be appreciated.
lbxCustomers.DataSource = lbxCustomers.Items.Cast<ListItem>().Reverse().ToList();
lbxCustomers.DataBind();
May be first you should store the list in generic collection and then sort it. Something like this:
List<ListItem> list = new List<ListItem>(lbxCustomers.Items.Cast<ListItem>());
list = list.OrderBy(li => li.Text).ToList<ListItem>();
lbxCustomers.Items.Clear();
lbxCustomers.Items.AddRange(list.ToArray<ListItem>());
Try resetting the DisplayMember and ValueMember properties of the Listbox after setting the DataSource. A lot of times if a control is already bound and you set the DataSource again, then it drops the DisplayMember/ValueMember properties.
Try sorting the list separately.
Point the Text value and Data value in the next step.
Databind the control.

Adding an element to 2 parent elements

I am trying to retreive data from XML and if variable1 == variable2, it will add the element (listboxitem) to 2 parent elements (listbox - listbox1, listbox2). I am trying to use the following code:
if (variable1 == variable2)
{
ListBox1.Items.Add(ListBoxItem);
ListBox2.Items.Add(ListBoxItem);
}
else
{
ListBox1.Items.Add(ListBoxItem);
}
I was thinking to get around this i may be able to duplicate the ListBoxItem but im not quite sure how.
Any help very much appreciated :D
Thanks!
What you could do is build two ObservableCollections containing references to items from the xml data you've queried.
Instead of applying the logic above to ListBoxItem, apply it to each reference in the xml data and add the data to collection1 / collection2.
Then just bind collection1 / collection2 to listBox1.ItemsSource and listBox2.ItemsSource.
You have two listboxes referencing the same data then without the problems of duplicating ui controls within the tree.
Any control in wpf and silverlight can only appear only once in the object tree. So you cannot add one ListBoxItem to several ListBoxes. You can create a "copy" this way
ListBoxItem itemToClone = ...
ListBoxItem clonedItem = new ListBoxItem();
clonedItem.Content = itemToClone.Content;
So when itemToClone.Content is not itself a control but a string or a number you'll have two ListBoxItems showing the same content.

Categories

Resources