I'm used to the old Winforms way of doing things. Apparently WPF ListViews are full of... XmlElements? How would I do something like disabling a ListViewItem?
foreach (XmlElement item in this.lvwSourceFiles.Items)
{
//disable?
}
ListView is an ItemsControl. ItemsControl.Items does not return the child controls - it returns the items - that is, objects that you have added to the ListView, either directly, or via data binding. I guess in this case you have bound your ListView to some XML, right?
ListViewItem (and other classes like it - e.g. ListBoxItem for ListBox) is called an "item container". To retrieve an item container for a given item, you should do this:
ListView lv;
...
foreach (object item in lv.Items)
{
ListViewItem lvi = (ListViewItem)lv.ItemContainerGenerator.ContainerFromItem(item);
}
You need to access the ListViewItem that represents the data item. You can achieve that through the ItemContainerGenerator
foreach (object item in this.lvwSourceFiles.Items)
{
UIElement ui = lvwSourceFiles.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
if (ui != null)
ui.IsEnabled = false;
}
You can Perform that In XAML Easily
Related
The project I am working currently is in wpf and I am newer to wpf. In my project I want to search the tree view and when match occurs I need to highlight that element. I worked like this and I failed to set the back ground color of the node.
foreach (object item in treeView1.Items)
{
cls.Node n=(cls.Node)item;
n.IsSelected = true;
}
Can anyone help me on this. the above code is a sample code.
You have to get the wrapper TreeViewItem to set the Background. Because TreeView does not use VirtualizingStackPanel in its ItemsPanelTemplate, so we can safely use the ItemContainerGenerator to get a TreeViewItem from some item:
foreach (object item in treeView1.Items) {
//cls.Node n=(cls.Node)item;
//n.IsSelected = true;
var tvItem = treeView1.ItemContainerGenerator.ContainerFromItem(item)
as TreeViewItem;
if(tvItem != null) tvItem.Background = Brushes.Blue;//just an example
}
I'm not sure if the original code (commented above) is your attempt or not, so just uncomment them if you want (cls.Node is actually your custom class).
I have a list of string with which I want to create a menu of ToolStripItem (neither the list size nor its content is know at debug) and I want each item to execute the same event, so I go through the list with that code:
foreach (string i in items)
{
ToolStripItem item = (ToolStripItem)toolStripMenuItem1.DropDownItems.Add(i);
item.Click += new EventHandler(item_Click);
}
the problem is that in the item_Click method I need to know which of the above items triggered the event. If I were in WPF, I would have used RoutedEventArgs and its .Source proprety but since I am only in a regular Windows Forms app, I'm not quite sure how to do it. Is there a simple way to do it?
Thank you.
Use sender parameter in your click event
var item = sender as ToolStripItem;
if(item != null)
{
...
}
I tried to manage on how to prevent double entries in ListView Item in C#. All of them didn't work for me.
I try to based the source code of Ahmad Mageed and I was confused of his trappings.
I based his source code to my project
ListViewItem item = ListView1.FindItemWithText(txtPLU.Text);
if (item != null)
{
MessageBox.Show("Item is already been exist!"); //Result if the item has exist in the listview item.
}
else
{
addToList(); //Its a method to add the product items in the ListViewItem.
txtBoxPLU.Focus();
}
The Behaviour of the runtime is that it only add an item.
Sorry if this is kinda confusing for all of you. I just to trap if the item is already exist in the listview item.
The ListView.ListViewItemCollection has 2 methods that can be used to find if an item is contained in the collection.
The Contains and ContainsKey method.
Example:
ListViewItem _item = new ListViewItem();
if (!listView1.Items.Contains(_item))
{
// TODO: Add to list.
}
else
{
// Already exists.
}
I've a common ContextMenuStrip for every workspace control of my application.
This ContextMenuStrip contains 4 Items ("Move front", "Move back", and "Delete control").
Now I want to extend it for one control.
There's a DataGridView on this control and I want an additional item to delete the selected DataGridViewRow.
This is the code I tried:
private void extendContextMenuOfDataGridViewRow (DataGridViewRow row) {
ContextMenuStrip ctx = new ContextMenuStrip();
foreach (ToolStripMenuItem item in this.ContextMenuStrip.Items) {
ctx.Items.Add(item);
}
ctx.Items.Add(new ToolStripSeparator());
ToolStripMenuItem ctxDeleteRow = new ToolStripMenuItem("Delete row");
ctxDeleteRow.Name = "ctxDeleteRow";
ctxDeleteRow.Click += new EventHandler(ctxDeleteRow_Click);
ctx.Items.Add(ctxDeleteRow);
row.ContextMenuStrip = ctx;
}
After the first item of the foreach loop was added to ctx.Items the debugger leaves the whole method and the first item is missing at the common ContextMenuStrip.
How do I do that right?
If you want to extend functionally of some control, you can either
a) create an extension method
public void DoSomething(this MyExtendedControl mec, DataGridViewRow row)
{
}
b) create a new class inheriting from your unsatisfactory control (or even create a completely new control), when you can override/add things as needed
Depends on your specific needs, couldn't understand from your description...
I haven't worked with WinForms for ages, but are you sure that you can keep the same Row object assigned to two different Strips at once?
foreach (ToolStripMenuItem item in this.ContextMenuStrip.Items) {
ctx.Items.Add(item);
}
I seriously doubt this should work by design because a row has to “talk” to its parent, and by adding it to another strip I'm afraid you're re-assigning the parent.
Instead, I would have added an item to the common menu but with its Visible property to false.
Then I would catch the menu opening event and make item visible if target is a DataGridViewRow.
I am trying to get a Listbox Item by below code. Basically what i am trying to do is create a tempdatelist and then set the itemsource of Listbox to tempdatelist.
if (App.Saveholidayplan[App.selectedlistindex].travel.Count > 0)
foreach (var dictobj in App.Saveholidayplan[App.selectedlistindex].travel[0].DummyRepository)
tempdatelist.Add(dictobj.Key);
lst_mainlist.ItemsSource = tempdatelist;
ListBoxItem item = this.lst_mainlist.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
//* item is alway null, that is the problem
if(item != null)
But in the above code item retuns null.
When i check Online some suggested to call ItemContainerGenerator.StatusChanged event.
But i am not able to find this event in WP7?
Is there a StatusChanged event in WP7 and if not what is the alternative?
I had this problem too. The solution is to wait for the UI to render by using the dispatcher, like this:
this.Dispatcher.BeginInvoke(() =>
{
ListBoxItem item = this.lst_mainlist.ItemContainerGenerator.ContainerFromIndex(i);
//...
});