How to bind listbox items using observable colletion in wp7? - windows-phone-7

My Code:
ObservableCollection<SampleCheckedData> interestrates = new ObservableCollection<SampleCheckedData>();
XDocument xmlDocu = XDocument.Load(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(result)));
interestrates = (from rts in xmlDocu.Descendants("Friend")
select new SampleCheckedData
{
Id = (string)rts.Element("userid"),
Name = (string)rts.Element("name"),
Icon = (string)rts.Element("imageurl"),
VisibleStatus = (string)rts.Element("visiblestatus"),
AppStatus = (string)rts.Element("loginstatus"),
imgBubble =bitmapRed,
}).ToList<SampleCheckedData>();
Then Getting Error as can't implicitly convert system.collection.generic.list to system.collection.observablecollection like that.How to bind listbox items using observable collection?
EDIT:
Button b = sender as Button;
var res = interestrates.Where(a => a.Id.Equals(((System.Windows.FrameworkElement)(e.OriginalSource)).Tag)).ToList();
if (res.Count == 1)
interestrates.Remove(res.First());
interestrates = new ObservableCollection<SampleCheckedData>();
lstFriendRequuest.ItemsSource = "";
bindGetFriends();
Here successfully deleting item from list but after calling bindGetFriends() in that binding the items newly then i am not getting new items getting old items.why the service returning old items list?

Use this extension:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public static class Extensions
{
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> collection)
{
var observableCollection = new ObservableCollection<T>();
foreach (var item in collection) observableCollection.Add(item);
return observableCollection;
}
}
Usage:
interestrates = (from rts in xmlDocu.Descendants("Friend")
select new SampleCheckedData
{
Id = (string)rts.Element("userid"),
Name = (string)rts.Element("name"),
Icon = (string)rts.Element("imageurl"),
VisibleStatus = (string)rts.Element("visiblestatus"),
AppStatus = (string)rts.Element("loginstatus"),
imgBubble =bitmapRed,
}).ToObservableCollection<SampleCheckedData>();

Change your Observable collection to List,
List<SampleCheckedData> interestrates = new List<SampleCheckedData>();
You can also bind List to ListBox, instead of ObservableCollection
And to solve your other problem of deleting selected item from listbox, try the following code:
var selectedIndex = listbox.SelectedIndex;
var listItems = listbox.ItemsSource as List<SampleCheckedData>;
listItems.RemoveAt(selectedIndex);
listbox.ItemsSource = null;
listbox.ItemsSource = listItems;
If still you are facing problems, let me know

Related

Receive items from a XAMARIN feed

With this command I search all the items of a url where he looks for 10 items, now I need with this command to select only the item from the fifth position,
Returning only one item, how can I do this using the command below, what changes should I make
private async Task<List<FeedItem>> ParseFeed(string rss)
{
return await Task.Run(() =>
{
var xdoc = XDocument.Parse(rss);
var id = 0;
return (from item in xdoc.Descendants("item")
let enclosure = item.Element("enclosure")
where enclosure != null
select new FeedItem
{
Title = (string)item.Element("title"),
Description = (string)item.Element("description"),
Link = (string)item.Element("link"),
PublishDate = DateTime.Parse((string)item.Element("pubDate")).ToUniversalTime().ToString("dd/MM/yyyy HH:mm:ss"),
Category = (string)item.Element("category"),
Mp3Url = (string)enclosure.Attribute("url"),
Image = (string)enclosure.Attribute("url"),
Color_category =Convert.ToString(int.Parse((string)item.Element("color")), 16).PadLeft(6, '0'),
Id = id++
}).ToList();
});
}
Use Skip() and Take()
return (from item in xdoc.Descendants("item")
...
}).Skip(4).Take(1).ToList();

Bind List to GridviewComboboxcolumn in telerik radgrindview (winform)

I have a generic List like
List<return> returnlist
class return
{
public string returnid {get; set;
...
public List<string> Vouchernumbers
}
I bind the returnlist to the telerik radgridview.
How can i bind the voucherlist to the GridviewComboboxcolumn for each row ?
I have bind the voucherlist to the combobox after radgridview_complete_binding.
You need to create the grid with columns and data
You need to add the combobox column, initialize it.. Please check you need to have a dataeditor in here
Assign the string to datasource
comboColumn.DataSource = new String[] { "Test1", "Test2"};
You can bind the collections too:
Binding list BindingList<ComboBoxDataSourceObject> list = new BindingList<ComboBoxDataSourceObject>();
ComboBoxDataSourceObject object1 = new ComboBoxDataSourceObject();
object1.Id = 1;
object1.MyString = "Test 1";
list.Add(object1);
ComboBoxDataSourceObject object2 = new ComboBoxDataSourceObject();
object2.Id = 2;
object2.MyString = "Test 2";
list.Add(object2);
colboCol2.DataSource = list;
radGridView1.Columns.Add(colboCol2);
create radcombobox and set datasource and add it to rad grid
eg :
GridViewComboBoxColumn col = new GridViewComboBoxColumn();
col.DataSource = DAL.ActiveDb.GetList<SalesRep>().ToList().OrderBy(x => x.RepName).Select(x => new { Id = x.Id, x.RepName });
col.DropDownStyle = RadDropDownStyle.DropDown;
col.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
col.DisplayMember = "RepName";
col.ValueMember = "Id";
col.FieldName = "RepId";
col.HeaderText = "Rep Name";
col.Width = 200;
//var t = gridColInfo.Where(x => x.ColumnName.ToLower() == "repid").FirstOrDefault();
//if (t != null)
//{
// col.Width = t.ColumnWidth;
//}
this.radGridBillwiseOpening.Columns.Add(col);

Checklist Box Selected Items from LinQ

I am writing the following line of code to extract the selected items in checklistbox.
ListItemCollection ChecklistBoxCollection = new ListItemCollection();
foreach (ListItem ChecklistBoxItem in ChecklistBox.Items)
if (ChecklistBox.Selected)
ChecklistCollection.Add(ChecklistBox);
Is there any way to get these items in LinQ?
There's no automatic conversion to ListItemCollection, but you can use AddRange to add the selected items at once. I'm not sure this is much of an improvement and may be slower because AddRange only takes an array.
ListItemCollection ChecklistBoxCollection = new ListItemCollection();
ChecklistBoxCollection.AddRange( checklistBox.Items
.Cast<ListItem>()
.Where( i => i.Selected )
.ToArray() );
I have been using these extension methods.
public static List<string> GetCheckedValues(this CheckBoxList list)
{
var values = new List<string>();
values.AddRange(from ListItem item in list.Items
where item.Selected
select item.Value);
return values;
}
public static List<string> GetCheckedTexts(this CheckBoxList list)
{
var values = new List<string>();
values.AddRange(from ListItem item in list.Items
where item.Selected
select item.Text);
return values;
}

simple dropdown list

i did not found simple dropdown list .
i'm new in mvc3
this part create and fill : ListMonitoringLicenseModelList = new List();
i fill this objectsList in another part and use it to fill MonitoringLicenseModelList
objectsList = new List(...fill objectsList...);
List<MonitoringLicenseModel>MonitoringLicenseModelList = new List<MonitoringLicenseModel>();
foreach (object o in objectsList)
{
string[] tmpString = o.ToString().Split('#');
int i = 0;
monitoringLicenseModel = new MonitoringLicenseModel();
monitoringLicenseModel.MonitoringID =Convert.ToInt32(tmpString[i++]);
monitoringLicenseModel.LicenseUI = tmpString[i++];
MonitoringLicenseModelList.Add(monitoringLicenseModel);
}
public ActionResult SchedulesIndex()
{
return View(MonitoringLicenseModelList);
}
how can i write dropdownlist to show this
name value
MonitoringID = LicenseUI
#model List<MonitoringLicenseModel>
...
#Html.DropDownList(
"SelectedMotoringID",
new SelectList(Model, "MonitoringID", "LicenseUI")
)

Iterating tables in a context and the properties of those tables

I'm iterating the tables of a context and then the properties of those tables to eager load all columns in a context. I received some help via another question, but I don't seem to be able to figure out how to iterate the column properties of the actual table.
Final working code:
public static void DisableLazyLoading(this DataContext context)
{
DataLoadOptions options = new DataLoadOptions();
var contextTables = context.GetType().GetProperties().Where(n => n.PropertyType.Name == "Table`1");
foreach (var contextTable in contextTables)
{
var tableType = contextTable.GetValue(context, null).GetType().GetGenericArguments()[0];
var tableProperties = tableType.GetProperties().Where(n => n.PropertyType.Name != "EntitySet`1");
foreach (var tableProperty in tableProperties)
{
ParameterExpression paramExp = Expression.Parameter(tableType, "s");
Expression expr = Expression.Property(paramExp, tableProperty.Name);
options.LoadWith(Expression.Lambda(expr, paramExp));
}
}
context.LoadOptions = options;
}
You're only getting the ProperInfos. You need to get the values from the PropertyInfos:
var tablePropertInfos = context.GetType().GetProperties().Where(
n => n.PropertyType.Name == "Table`1");
foreach (var tablePropertyInfo in tablePropertInfos)
{
// Get the actual table
var table = tablePropertyInfo.GetValue(context, null);
// Do the same for the actual table properties
}
Once you have the PropertyInfo class, you need to get the value using the GetValue method.

Resources