this is my first experience on windows phone 8 application development.
I have a problem which I mention below, could you please help me to figure out it.
I show data from ActivityViewModel via Data Binding in Activities.xaml
When user tap to activity item, I send ActivityID to ActivityDetails.xaml.
I am trying to call object which I created as AccountAction by using ActivityID.
How can I get object values (ActivityImage, ActivityName, ActivityAmont ...) by using ActivityID in ActivityDetails.xaml.cs?
ActivityViewModel:
public class ActivityViewModel
{
public ObservableCollection<AccountAction> ActivityItemCollection { get; set; }
public ActivityViewModel()
{
ActivityItemCollection = new ObservableCollection<AccountAction>();
ActivityItemCollection.Add(new AccountAction()
{
ActivityID = "SP0001",
ActivityImage = "/Images/Logos/e-bay.png"
ActivityName = "E-Bay",
ActivityAmount = "100,00",
ActivityDate = "Today",
ActivityHour = "11:49"
});
.
.
.
}
}
Activities.xaml.cs
private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var _ActivityID = Convert.ToString(((StackPanel)sender).Tag);
NavigationService.Navigate(new Uri("/ActivityDetails.xaml?ActivityID=" + _ActivityID, UriKind.Relative));
}
ActivityDetails.xaml.cs
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string ActivityID = "";
if (NavigationContext.QueryString.TryGetValue("ActivityID", out ActivityID))
{
ReferanceNumber.Text = ActivityID;
}
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string ActivityID;
if (e.IsNavigationInitiator && this.NavigationContext.QueryString.TryGetValue("ActivityID", out ActivityID))
{
ActivityID= int.Parse(ActivityID);
}
}
EDIT:
You can get the data related to activityid by using linq
AccountAction result = ActivityItemCollection.FirstOrDefault(act=>act.ActivityID == ActivityID);
Then you can get object values like result.ActivityName
Related
I am writing a SharePoint app. There I have page with drop down list. I have
a handler for SelectedIndexChanged. I want to get the selected value but as CustomObject and the only option I see is string. I tried SelectedValue and it is still string.
That's how I set the list:
protected void Page_Load(object sender, EventArgs e)
{
List<CustomObject> customList = //retrieving data
myDropDownList.DataSource = customList.Select(x => new { x.Name, Value = x});
myDropDownList.DataTextField = "Name";
myDropDownList.DataValueField = "Value";
myDropDownList.DataBind();
}
And that's one of the ways I tried:
protected void myDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
var index = groupingDropDownList.SelectedIndex;
CustomObject obj = (CustomObject)myDropDownList.Items[index].Value;
obj.DoSomething();
}
Is it even possible? Or do I have to have somewhere Dictionary with with objects?
You will want to leverage the html5 data attributes that you can then place onto the dropdown options. Here is an example of what you could do with your data.
// add Agencies' addresses as HTML5 data-attributes.
var agencies = agencyNames.ToList();
for (int i = 0; i < requesting_agency.Items.Count - 1; i++) {
requesting_agency.Items[i + 1].Attributes.Add("data-address",
agencies[i].address);
servicing_agency.Items[i + 1].Attributes.Add("data-address",
agencies[i].address);
}
Then when processing the information you could do something like so.
var index = groupingDropDownList.SelectedIndex;
var selectedText = myDropDownList.Items[index].SelectedValue;
var selectedValue = myDropDownList.Items[index].Attributes["attribute"];
// put into obj
// do something with object
Let me know if you have any questions.
You 're binding a object (x => new { x.Name, Value = x}) to dropdown value, you should bind actual value to it.
Test demo:
public class CustomObject
{
public int ID { get; set; }
public string Name { get; set; }
public CustomObject(int _ID,string _Name)
{
this.ID = _ID;
this.Name = _Name;
}
}
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<CustomObject> customList = new List<CustomObject>();
customList.Add(new CustomObject(1,"test1"));
customList.Add(new CustomObject(2,"test2"));
myDropDownList.DataSource = customList.Select(x => new { x.Name, Value = x.ID });
myDropDownList.DataTextField = "Name";
myDropDownList.DataValueField = "Value";
myDropDownList.DataBind();
}
}
Currently, in course, I am trying to check the LandCode from the class Landen to get the cities from the selectedItem land, but I am parsing something wrong.
public partial class Landen
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Landen()
{
this.Steden = new HashSet<Steden>();
this.Talen = new HashSet<Talen>();
}
public string LandCode { get; set; }
public string Naam { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Steden> Steden { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Talen> Talen { get; set; }
}
public MainWindow()
{
InitializeComponent();
var context = new LandenStedenTalenEntities();
landenListBox.ItemsSource = (from Landen in context.Landen select Landen.Naam).ToList();
}
private void landenListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (var entities = new LandenStedenTalenEntities())
{
List<string> steden = new List<string>();
var landcode = ((Landen)landenListBox.SelectedItem).LandCode.ToString();
var gekozenland = entities.Landen.Find(landcode);
foreach(var stad in gekozenland.Steden)
{
steden.Add(stad.Naam);
}
stedenInLandenListBox.ItemsSource = steden.ToList();
}
}
Exception:
Unable to cast object of type 'System.String' to type 'TestEFDieter.Landen'.
I want to add them to a list and show them in a second Listbox.
Can anyone help me out? Thank you.
I would suggest you modify the code inside of the constructor so that the landenListBox will contain actual Landen object and displays only the Naam as it's item.
Change the code in the constructor to this:
public MainWindow()
{
InitializeComponent();
var context = new LandenStedenTalenEntities();
landenListBox.ItemsSource = context.Landen.ToList();
landenListBox.DisplayMemberPath = "Naam";
}
Adding DisplayMemberPath will inform ListBox to display that particular property as an item instead of calling ToString() method on that object.
Now in your later code you do not have to change much, just remove ToList() and since you're using EntityFramework you should insert the whole model in it's Find() method but it's useless since you already have that object loaded. You can just retrieve stad from it directly and display it in the same way Landen is displayed:
private void landenListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var landen = landenListBox.SelectedItem as Landen; // safe cast just in case
if (landen != null && landen.Steden != null ) // null checks
{
stedenInLandenListBox.ItemsSource = landen.Steden.ToList(); // in case it's proxy object
stadenInLandenListBox.DisplayMemberPath = "Naam";
}
}
I suppose you want to get that instance of Landen which corresponds the selected item in your list. As the elements in the listbox are just strings that represent the Naam-property of every Landen, you could simply iterate your list of Landen and get that one with the desired Naam:
var selectedLanden = landenList.FirstOrDefault(x => x.Naam == landenListBox.SelectedItem);
if(selectedLanden != null)
{
var landCode = selectedLanden.LandCode;
// ...
}
However as selectedLanden already is an instance of Landen, you won´t need to find it again by its LandCode. Thus your code boils donw to this:
List<string> steden = new List<string>();
var selectedLanden = landenList.FirstOrDefault(x => x.Naam == landenListBox.SelectedItem);
if(selectedLanden != null)
{
foreach(var stad in selectedLanden.Steden)
{
steden.Add(stad.Naam);
}
}
stedenInLandenListBox.ItemsSource = steden.ToList();
or a bit shorter:
stedenInLandenListBox.ItemsSource = selectedLanden.SelectMany(x => x.Steden.Select(y => y.Naam)).ToList();
For this to work you of course have to store a reference to the list of Landen somewehere in your class:
class MainWindow
{
List<Landen> landenList;
public MainWindow()
{
InitializeComponent();
this.landenList = new LandenStedenTalenEntities();
landenListBox.ItemsSource = (from Landen in this.landenList select Landen.Naam).ToList();
}
}
I am trying to add some controls to a list but for some reason its only saving the last view to the db.
public List<tblPortalIandEValue> createIandERecord(Guid _userId, string fieldName, string fieldValue)
{
List<tblPortalIandEValue> _iandevalues = new List<tblPortalIandEValue>();
_iandevalues.Add(new tblPortalIandEValue { userId = _userId, field_name = fieldName, field_value = fieldValue });
return _iandevalues;
}
I think my problems lies in my save cause I'm calling this list add new and then returning it think I should be returning the record instead
protected void btnSave_Click(object sender, EventArgs e)
{
List<tblPortalIandEValue> _iandevalues = new List<tblPortalIandEValue>();
_customerId = Guid.NewGuid();
_iandevalues=_dal.createIandERecord(_customerId , "mortagecic", mortagecic.Value.ToString());
_iandevalues = _dal.createIandERecord(_customerId, "gascic", gascic.Value.ToString());
_iandevalues = _dal.createIandERecord(_customerId, "electricitycic", electricitycic.Value.ToString());
_iandevalues.ForEach(n =>_dal.portalEntities.tblPortalIandEValues.Add(n));
_dal.portalEntities.SaveChanges();
}
Your problem is in your createIandERecord method since you are renewing the list everytime. change your code to this:
public tblPortalIandEValue createIandERecord(Guid _userId, string fieldName, string fieldValue)
{
return new tblPortalIandEValue { userId = _userId, field_name = fieldName, field_value = fieldValue });
}
protected void btnSave_Click(object sender, EventArgs e)
{
List<tblPortalIandEValue> _iandevalues = new List<tblPortalIandEValue>();
_customerId = Guid.NewGuid();
_iandevalues.Add(_dal.createIandERecord(_customerId, "mortagecic", mortagecic.Value.ToString()));
_iandevalues.Add(_dal.createIandERecord(_customerId, "gascic", gascic.Value.ToString()));
_iandevalues.Add(_dal.createIandERecord(_customerId, "electricitycic", electricitycic.Value.ToString()));
_iandevalues.ForEach(n => _dal.portalEntities.tblPortalIandEValues.Add(n));
_dal.portalEntities.SaveChanges();
}
I'm developping a Windows 8 Store Application (c#).
I have a Combobox (cboTeam1) that gets the items from a repository.
private static List<TeamItem> JPLItems = new List<TeamItem>();
public static List<TeamItem> getJPLItems()
{
if (JPLItems.Count == 0)
{
JPLItems.Add(new TeamItem() { Id = 1, Description = "Anderlecht", Image = "Jpl/Anderlecht.png", ItemType = ItemType.JPL });
JPLItems.Add(new TeamItem() { Id = 1, Description = "Beerschot", Image = "Jpl/Beerschot.png", ItemType = ItemType.JPL });
JPLItems.Add(new TeamItem() { Id = 1, Description = "Cercle Brugge", Image = "Jpl/Cercle.png", ItemType = ItemType.JPL });
JPLItems.Add(new TeamItem() { Id = 1, Description = "Charleroi", Image = "Jpl/Charleroi.png", ItemType = ItemType.JPL });
}
return JPLItems;
}
I load the items in the cboTeam1's ItemsSource:
cboTeam1.ItemsSource = ItemRepository.getJPLItems();
When cboTeam1 selectionchanged I do this:
private void cboTeam1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Ploeg1.Text = cboTeam1.SelectedValue.ToString();
}
This results in: SportsBetting.Model.TeamItem
Can anyone help me to get the combobox selectedvalue in my textblock (Ploeg1.Text)??
You've nearly answered this for yourself.
private void cboTeam1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// cast the selected item to the correct type.
var selected = cboTeam.SelectedValue as TeamItem;
//then access the appropriate property on the object, in this case "Description"
// note that checking for null would be a good idea, too.
Ploeg1.Text = selected.Description;
}
The other option would be to override ToString() in your TeamItem class to return Description. In that case your original code should work fine.
public override string ToString()
{
return this._description; // assumes you have a backing store of this name
}
I have a class which will act as variables to store data from textboxes:
public class Business
{
Int64 _businessID = new Int64();
int _businessNo = new int();
string _businessName;
string _businessDescription;
public Int64 BusinessID
{
get { return Convert.ToInt64(_businessID.ToString()); }
}
public int BusinessNo
{
get { return _businessNo; }
set { _businessNo = value; }
}
public string BusinessName
{
get { return _businessName; }
set { _businessName = value; }
}
public string BusinessDescription
{
get { return _businessDescription; }
set { _businessDescription = value; }
}
I then have the code to store the data from the textbox into a session and into a list (there can be many businesses uploaded to the database at one time) - database irrelevent for now. I then want to display the list of businesses stored into the session onto the gridview: (b = class business)
List<Business> businessCollection = new List<Business>();
protected List<Business> GetBusinesses()
{
return (List<Business>)Session["Business"];
}
protected void btnRow_Click(object sender, EventArgs e)
{
if (Session["Business"] != null)
businessCollection = (List<Business>)Session["Business"];
Business b = new Business();
b.BusinessNo = Convert.ToInt32(txtBNo.Text);
b.BusinessName = txtBName.Text;
b.BusinessDescription = txtBDesc.Text;
businessCollection.Add(b);
GridView1.DataSource = GetBusiness();
GridView1.DataBind();
}
It doesn't seem to add the list to the gridview, can someone help?
Debug your code and ensure that if (Session["Business"] != null) actually evaluates to true.
If it is false then you are adding to a list that is never returned from GetBusinesss
Without any more information you can rewrite it like this:
List<Business> businessCollection = new List<Business>();
protected List<Business> GetBusinesses()
{
if (Session["Business"] == null)
return businessCollection;
else
return (List<Business>)Session["Business"];
}
protected void btnRow_Click(object sender, EventArgs e)
{
Business b = new Business();
b.BusinessNo = Convert.ToInt32(txtBNo.Text);
b.BusinessName = txtBName.Text;
b.BusinessDescription = txtBDesc.Text;
var currentCollection = GetBusinesses();
currentCollection.Add(b);
GridView1.DataSource = currentCollection;
GridView1.DataBind();
}
I personally wouldn't do it like this, as it seems like you need an assignment to Session["Business"] but I don't want to change the logic of your code.
Update
I wanted to update this with what I think you wanted to accomplish.
protected List<Business> GetBusinesses()
{
if (Session["Business"] == null)
Session["Business"] = new List<Business>();
return (List<Business>)Session["Business"];
}
protected void btnRow_Click(object sender, EventArgs e)
{
Business b = new Business();
b.BusinessNo = Convert.ToInt32(txtBNo.Text);
b.BusinessName = txtBName.Text;
b.BusinessDescription = txtBDesc.Text;
var currentCollection = GetBusinesses();
currentCollection.Add(b);
GridView1.DataSource = currentCollection;
GridView1.DataBind();
}
It seems you are not assigning anything to Session["Business"]
There's a very strong chance that you're problem is caused by the fact that you are referencing the Business List object inconsistently. You've created an accessor for this object, so use it everywhere.
This:
if (Session["Business"] != null)
businessCollection = (List<Business>)Session["Business"];
Should be:
var businessCollection = GetBusiness();
Note the use of var: I suspect defining businessCollection as a member variable is part of the problem. In any case it is bad design if your intent is to store the list in the session. So I would also remove the member declaration for businessCollection and stick with a locally scoped variable.