how to retrieve All Checked Items from radTreeListView SelectedItems Collection - c#

how do i retrieve all Checked Items from a radTreeListView SelectedItems Collection in c#?
The example below from Retrieve All Checked Items isn't working at all.
foreach ( object checkedItem in radTreeView.CheckedItems )
{
// Get the container(RadTreeViewItem) of the checked item
RadTreeViewItem container = radTreeView.ContainerFromItemRecursive( checkedItem );
// Add your logic for handling the checked item scenario here
}
The XML for the RadTreeListView is
<telerik:RadTreeListView x:Name="radTreeListView" ItemsSource="{Binding Items}"
AutoGenerateColumns="False" Grid.RowSpan="2" SelectionChanged="radTreeListView_SelectionChanged" SelectionMode="Extended" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" SelectionChanging="radTreeListView_SelectionChanging" BeginningEdit="radTreeListView_BeginningEdit">
<telerik:RadTreeListView.ChildTableDefinitions>
<telerik:TreeListViewTableDefinition ItemsSource="{Binding Items}" />
</telerik:RadTreeListView.ChildTableDefinitions>
<telerik:RadTreeListView.Columns>
<telerik:GridViewSelectColumn/>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" />
</telerik:RadTreeListView.Columns>
</telerik:RadTreeListView>

SelectedItems is the key!
I prepared SampleData as described here and capture checked items with following code.
System.Collections.ObjectModel.ObservableCollection<object> selected_items = radTreeListView.SelectedItems;
foreach (object item in selected_items)
{
WarehouseItem warehouseitem = (WarehouseItem)item;
MessageBox.Show(warehouseitem.Name);
}

Related

Unable to cast object of type ''System.Xml.Linq.XElement' to type 'System.Data.DataRowView'

When I want to get value from DataGrid selected row I had error Unable to cast object of type ''System.Xml.Linq.XElement' to type 'System.Data.DataRowView'.
Date in DataGrid is load from XML file.
C#:
foreach (DataRowView row in dataGrid.SelectedItems)
{
string text = row.Row.ItemArray[0].ToString();
MessageBox.Show(text);
}
XAML:
<DataGrid Name="dataGrid" ItemsSource="{Binding Path=Elements[person]}" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="331" Margin="10,10,0,0" VerticalAlignment="Top" Width="1130" FontSize="14" FontFamily="SimSun">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Element[name].Value}" />
<DataGridTextColumn Header="Date" Binding="{Binding Path=Element[date].Value}" />
</DataGrid.Columns>
</DataGrid>
If you have bound the ItemsSource property to an IEnumerable<XElement>, you won't get any DataRowView objects from the SelectedItems collection. These are XElements and nothing else.
How to get the value of an XElement depends on how your XML data is structured, but the following sample code should give you the idea:
foreach (var row in dataGrid.SelectedItems.OfType<XElement>())
{
XElement nameElement = row.Element("name");
if(nameElement != null)
{
string name = nameElement.Value;
}
}

GetItemAt returning null value instead of ComboBoxItem

I have two ComboBox with same items.
I am trying to get the ComboBoxItem of a ComboBox by Index but NULL value is being returned.
My code is:
var index = comboBox1.SelectedIndex;
ComboBoxItem item = comboBox2.Items.GetItemAt(index) as ComboBoxItem; // item is null here
//item = (ComboBoxItem)comboBox2.ItemContainerGenerator.ContainerFromItem(comboBox1.SelectedItem);
//also tried above line but same result(null)
And XAML:
<ComboBox Name="comboBox1" ItemsSource="{Binding ExistingModuleGroups}" SelectedItem="{Binding SelectedModuleGroup}" SelectionChanged="ComboBox1_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox Name="comboBox2" ItemsSource="{Binding ExistingModuleGroups}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
if (comboBox2.Items.Count > 0)
{
var index = comboBox1.SelectedIndex;
ComboBoxItem item = comboBox2.Items.GetItemAt(index) as ComboBoxItem; // item is null here
//item.IsEnabled = false;
}
}
}
Any idea...
The ItemsControl.Items property stores the actual data, not the generated ComboBoxItems (unless you've manually added objects of type ComboBoxItem to the Items collection).
You were close with that second piece of code you've commented, but you were looking for an item from the first combo in the second combo. Since you are probably not using the same instances for both combos, that won't work.
The correct thing would probably be this. Similar to what you've already tried, but with some key differences:
var index = comboBox1.SelectedIndex; // Get the index from the first combo
var item = (ComboBoxItem)comboBox2.ItemContainerGenerator
.ContainerFromIndex(index); // And get the ComboBoxItem from that index
// in the second combo

Datagrid ComboBox binding two values

I have a combobox which is populated from a list of names which were obtained from selecting from an Observable collection. However, associated with those names is an ID also in that Observable collection. The goal is when the user selects a new name (Say changes "John" to "Jill") I will be able to obtain the ID, not just the name. The only way I can think of doing this is storing the ID also in the combobox somehow. But I don't know how to do that with binding.
<DataGridTemplateColumn Header="Name ">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="namescombo" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Names}"
SelectedItem="{Binding Name, UpdateSourceTrigger=PropertyChanged}" FontSize="12" Background="White" FontFamily="Cambria" BorderBrush="White" BorderThickness="0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
C#
ObservableCollection<Name> Names = new ObservableCollection<Name>();
Name twofields = new Name();
var NamesQuery =
from p in dataEntities.Names
select new { p.Name, p.Id };
foreach (var p in NamesQuery)
{
Names.Add(new Name
{
ID = p.Id,
Name = p.Name
});
}
Names = Names.Select(p => p.Name).Distinct().ToList();
A ComboBox contains properties for both the DisplayMemberPath and the SelectedValuePath, so you could use it to tell the ComboBox to identify items by the "Id" property, but display the "Name" property to the user.
For example,
<DataGridTemplateColumn Header="Name ">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Names}"
DisplayMemberPath="Name"
SelectedValuePath="Id"
SelectedValue="{Binding SelectedId}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I would recommend using SelectedValue over SelectedItem because WPF compares SelectedItem by .Equals() which defaults to comparing items by reference, and if your SelectedItem is not the exact same reference as the item in your ItemsSource, it won't get selected.
For example, SelectedItem = new Person(1, "Test"); would probably not set the selected item correctly, while SelectedItem = ItemsSource[0] would since it refers to an item that exists in the ItemsSource.
Also, it frequently makes more sense to store just the Id of the selected item on a row instead of the entire object :)
You can bind directly to Name object collection and set DisplayMemberPath to Name property so that strings are shown on GUI but in essence you have complete object binded to comboBox.
This way you can bind SelectedItem to Name object and can access Id and Name property.
<ComboBox ItemsSource="{Binding Names}" // Collection of name objects.
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedNameObject}"/> // Object of type Name.

how to Remove item from one Listbox and add into another Listbox?

I am trying to remove selected item from one listbox and add that into new listbox as follows code
Binding on PageLoad:
Getting the records from database into a DataTable and bind that to Listbox.
lstBox1.DataContext = dtData;
Code Bind:
List<object> _selecteditems = new List<object>();
foreach (var item in lstBox1.SelectedItems)
{
_selecteditems.Add(item);
}
foreach (var item in _selecteditems)
{
lstBox1.Items.Remove(item);
lstBox2.Items.Add(item);
}
Design:
<ListBox Name="lstBox1" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ID}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Name="lstBox2" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ID}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I am getting an error when removeing the item "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
Don't manipulate the items via the Items property, instead add/remove items from the List that the ListBox is bound to:
<ListBox Name="lstBox1" ItemsSource="{Binding myListProperty}">
...etc...
</ListBox>
If your myListProperty returns a collection that implements INotifyCollectionChanged (like the ObservableCollection does) then the listboxes will automatically show the new items as they are added and the removed items will instantly disappear.
I tried by following way and its working for me.
List<object> _selecteditems = new List<object>();
foreach (var item in lstBox1.SelectedItems)
{
_selecteditems.Add(item);
}
foreach (var item in _selecteditems)
{
DataRow dr = dtSelctedDest.NewRow();
dr[0] = ((DataRowView)item).Row[0].ToString();
dr[1] = ((DataRowView)item).Row[1].ToString();
dtSelctedItem.Rows.Add(dr);
dtAllItem.Rows.Remove(dtAllItem.Rows.Remove.Select(string.Format("ID='{0}'", ((DataRowView)item).Row[0].ToString()))[0]);
}
lstBox1.DataContext = dtAllItem;
lstBox2.DataContext = dtSelctedItem;

Help binding my silverlight datagrid

I have an entity called Incident, as you can see on the picture, it holds a list with several bugs.
Then I have a datagrid that I connect a list with all my incident to:
List<ExtendedIncident> allIncidents;
myGrid.ItemsSource = allIncidents;
Now I bind some of the values from every incident, like this in the xaml code:
<sdk:DataGrid AutoGenerateColumns="False" Name="grid" SelectionMode="Single" SelectionChanged="grid_SelectionChanged">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Incident">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<HyperlinkButton Content="{Binding CallId}" Click="HyperlinkButton_Click"></HyperlinkButton>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Header="Beskrivnig" Binding="{Binding Description}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="Beskrivnig" Binding="{Binding Status}"></sdk:DataGridTextColumn>
</sdk:DataGrid.Columns>
My problem now is that I would like to add some columns that presents some data from the bugs, that are relevant (stored in the list for the incident) in the same row. How can I present data from the list of buggs from the incident?
Would be very thankful for any help
Use RowDetailsTemplate:
<sdk:DataGrid x:Name="dataGrid1" Height="400" IsReadOnly="True" >
<sdk:DataGrid.RowDetailsTemplate>
<DataTemplate>
<sdk:DataGrid ItemsSource={Binding BuggItems}/>
</DataTemplate>
</sdk:DataGrid.RowDetailsTemplate>
</sdk:DataGrid>
Or alternatively a property that aggregates all the items stored in list (below an example if IDs are strings).
public partial class Incident
{
public string IDs
{
get
{
return BuggItems.Aggregate((a,b) => a + "," + b);
}
}
}

Categories

Resources