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.
Related
I have a combobox binded to an observable collection through
cmbBladesTab1.ItemsSource = easyRunData.olstBlades;
that works fine.
I want the combobox to be binded to all that values plus one.
E.g.
easyRunData.olstBlades; contains "PL1", "PL2", "PL3", "PL4"
while cmbBladesTab1 contains "ALL BLADES", "PL1", "PL2", "PL3", "PL4"
--ADD all work has to be done from code-behind
Thanks for your help.
You could add a property, that adds the particular item to the list.
ObservableCollection<string> myCollection;
ObservableCollection<string> MyCollectionViewProp
{
get
{
var tempCollection = new ObservableCollection<string>(myCollection);
tempCollection.Add("Extra element");
return tempCollection;
}
}
Depending on the size of the collection and the number of times it is accessed, this is probably the programmatically simplest solution. If you need to access it often, the worse this solution gets, as it creates a new collection every time.
In this case you should probably listen to the CollectionChanged event and keep a separate redundant list.
Easiest way would be to add an extra item in the observable collection with some prefixed text / key.
That way, because it's in the collection, it will be visible in the combobox and when the user selects this item you can evaluate it to see if it's the added item or not.
A good example is indeed given as an answer on this question add an item to combobox before bind data from data base
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
Here's my problem: I need to make a DataGrid with dynamic comboboxes using the WPF. If the value of a combobox is already used in the previous rows, the next ones, that will be added by the user, shouldn't contain the item already used.
In this image, the ITEM A shouldn't apear on the combobox of the second line.
I don't have ideia how to accomplish this, can anyone show me a light?
OBS: The DataGrid ItemsSource is binded to an ObservableCollection, and the DataGridComboBoxColumn ItemsSource is a List.
Thanks !!!
The ItemsSource of the combo doesn't have to be bound to an ObservableCollection, but it can help depending on exactly how you solve this.
When that cell goes in to edit mode the property the ItemsSource is bound to gets hit - so you can return a new list of items each time the getter is hit. Here is a very basic example to give you an idea:
public List<string> MyItemsSource
{
get
{
var myNewList = MyMasterList.ToList(); //create a (reference) copy of the master list (the items are not copied though, they remain the same in both lists)
if (PropertyA != null)
myNewList.Remove(PropertyA);
return myNewList;
}
}
So what you are creating and returning is a filtered version of your master list of all possible items. LINQ will be of great help to you here.
Alternatively you could keep just one static copy of the master list as an ObservableCollection, and simply remove items from that static copy as they get selected (and add them back in as they get unselected). Which option you choose will depend on how many times the list can be modified due to items being selected and how complicated it is to generate the list. I've used the dynamically generated list many times in the past, it's an option that works well in most cases.
WPF treeview. Is there a way to add a child node to an already populated treeview without having to run in a for/foreach to check the header then converting that into an TreeViewIem ?
private void AddChildNode(string _rootNode, string _childeNode)
{
foreach (TreeViewItem node in tvSQLTasks.Items)
{
if (node.Header.Equals(_rootNode))
{
node.Items.Add(new TreeViewItem() { Header = _childeNode });
}
}
}
Create an ObservableCollection collection of objects, populate the collection with objects representing what treeview is supposed to display and bind that collection to ItemSource property of your TV.
Binding is the only proper way to go about populating your treeview with items in WPF and if you use the ObservableCollection you'll have the added benefit of items added to/removed from the collection "automagically" appearing in/disappearing from your TV without writing any additional code.
Depending on how complex your treeview needs to be you might have to use HierarchicalDataTemplate and ItemStyleSelector.
I have 2 listboxes each displaying 2 different lists which are being populated by user input. I was wondering if I could somehow combine the data in each listbox and show it in a third listbox. This is a windowsForms application in visual Studio. I also want to make sure that it is updated properly when a new value is added into the 2 different listboxes. So far what I have done is combined the two lists that I have as so:
public List<String> listAll()
{
List<String> all = new List<string>();
all.AddRange(listFirstName());
all.AddRange(listSecondName());
return all;
}
The problem with this is first of all i dont know if this will update when a new value is added to the other two lists. and secondly now that I have this new list i still dont know how to display it in a listbox. keep in mind that i still need to have the other listboxes containing the first 2 lists displayed in the main form along with this new listbox which will contain the values for both of them.
Cheers, any help is welcome and appreciated.
You can have seperate method to populate listBox3. in that method you can clear existing items and add all the items from listBox1 and listBox3.
when you add items to listBox1 or listBox2 you can call same method after adding items.
listBox3.Items.Clear();
listBox3.Items.AddRange(listAll().ToArray());
Usually, if you want to make sure that your Listbox updates dynamically, use an ObservableCollection instead of a List.
ObservableCollection has the same format as a List:
ObservableCollection<String> all = new ObservableCollection<string>();
Make sure you add the following
using System.Collections.ObjectModel;