locked
How to reach Sample Data via C# code ? RRS feed

  • Question

  • Hello,

    I'm wondering how to reach the sample data collections via (C#) code.

    In fact my setup is the following:

    - I'm creating a prototype app using Sketchflow
    - On one of the screens is a "seach form"
      - This "search form" is put in a "Component Screen", so I could reuse it on a "search results page"
      - This "search form" contains several dropdowns, and 1 AutoCompleteBox, which I styled sketchy
      - The AutoCompleteBox should "react" on one of 2 fields, one is a number, the other is a text
      - To archieve this I bound those 2 fields from some SampleData I imported via an XML file
      - I adapted DataTemplate so that only those 2 fields should show up in the list while typing
      - then I found some code to which the AutoCompleteBox.ItemFilter should be bound
        - this is necessary as I want the AutoCompleteBox to react on one of 2 instead of just 1 field... so far so good, but !
        - the code (which you can find below) should cast the "item" to one of the objects from the SampleDataCollection
        - then the AutoCompleteBox can figure out if "searchfor" is located in one of those 2 fields

    myAutoCompletebox.ItemFilter = (searchfor, item) =>
    {
      ObjectFromSampleDataCollection myObject = (ObjectFromSampleDataCollection) item;
    
    return (myObject.myField.ToString().Contains(searchfor) || myObject.myField.ToString().Contains(searchfor));
    };
    

    The problem I have is that I don't know how to reach that "object" from the SampleDataCollection (here type : ObjectFromSampleDataCollection)...

    I've been looking all weekend how that object could be called but I can't find it...

    please help !

    Monday, November 15, 2010 6:26 AM

Answers

  • I'm no expert, but I stumbled on your question when looking for the answer myself, albeit a slightly different problem (trying to get the selectedItem from a listbox that was using a DataTemplate of 2 fields). Printing out item.GetType().ToString() is what really helped me make progress. I think your looking for something like: Expression.Blend.SampleData.[SampleDataSource].[Property]Item.

    Autocomplete was a disappointment, and #using the SampleData namespace left me hanging, so I'll just dump the details of my scenario and see if you can't work backwards from it. My SampleDataSource looked something like this:

    AddressBook (sample data source)
    -AddressBook
    --Groups (collection)
    ---Count (number)
    ---Name (string)

    Both Count and Name were databound to a ListBox DataTemplate, and this is the code that extracted the name from the SelectedItem.

    private void onGroupSelect(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
    	ListBox l = (ListBox) sender;
    	Expression.Blend.SampleData.AddressBook.GroupsItem g = (Expression.Blend.SampleData.AddressBook.GroupsItem) l.SelectedItem;
    	who.Text = String.Format("{0} {1}", who.Text, g.Name);
    } 
    

    Hopefully there is a shred of helpful information in that.

    • Marked as answer by modul8com Monday, November 15, 2010 9:37 PM
    Monday, November 15, 2010 8:56 AM
  • The actual c# code for the sampledata classes gets added to your project, I believe in the SampleData folder. Open that and have a look for the class name. You will also likely need the namespace from that file to add to the using statements in your code behind class.
    • Proposed as answer by Chuck HaysModerator Monday, November 15, 2010 2:05 PM
    • Marked as answer by modul8com Monday, November 15, 2010 9:36 PM
    Monday, November 15, 2010 2:05 PM
    Moderator

All replies

  • I'm no expert, but I stumbled on your question when looking for the answer myself, albeit a slightly different problem (trying to get the selectedItem from a listbox that was using a DataTemplate of 2 fields). Printing out item.GetType().ToString() is what really helped me make progress. I think your looking for something like: Expression.Blend.SampleData.[SampleDataSource].[Property]Item.

    Autocomplete was a disappointment, and #using the SampleData namespace left me hanging, so I'll just dump the details of my scenario and see if you can't work backwards from it. My SampleDataSource looked something like this:

    AddressBook (sample data source)
    -AddressBook
    --Groups (collection)
    ---Count (number)
    ---Name (string)

    Both Count and Name were databound to a ListBox DataTemplate, and this is the code that extracted the name from the SelectedItem.

    private void onGroupSelect(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
    	ListBox l = (ListBox) sender;
    	Expression.Blend.SampleData.AddressBook.GroupsItem g = (Expression.Blend.SampleData.AddressBook.GroupsItem) l.SelectedItem;
    	who.Text = String.Format("{0} {1}", who.Text, g.Name);
    } 
    

    Hopefully there is a shred of helpful information in that.

    • Marked as answer by modul8com Monday, November 15, 2010 9:37 PM
    Monday, November 15, 2010 8:56 AM
  • The actual c# code for the sampledata classes gets added to your project, I believe in the SampleData folder. Open that and have a look for the class name. You will also likely need the namespace from that file to add to the using statements in your code behind class.
    • Proposed as answer by Chuck HaysModerator Monday, November 15, 2010 2:05 PM
    • Marked as answer by modul8com Monday, November 15, 2010 9:36 PM
    Monday, November 15, 2010 2:05 PM
    Moderator
  • That was it, both you guys helped me to find the correct answer:

    This is a part of what's in the XAML file in the SampleData directory :

    <SampleData:dataroot xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.tblCostcentersSampleDataSource" generated="2010-10-27T19:05:58" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
     <SampleData:dataroot.tblCostcentersCollection>
      <SampleData:tblCostcenters CostcenterId="1" Name="*N/A" />
      <SampleData:tblCostcenters CostcenterId="1000" Name="OSSEGHEM (global)" />
    
    

    I tried as RGNEVER states the : "Expression.Blend.SampleData."-way to reach it, but it didn't work well in Expression Blend, Visual Studio 2010 (Express) on the other hand gave me more help, and finally this is the code I needed to produce:

    Expression.Blend.SampleData.tblCostcentersSampleDataSource.tblCostcentersCollection.tblCostcenters cc = (Expression.Blend.SampleData.tblCostcentersSampleDataSource.tblCostcentersCollection.tblCostcenters) item;
    

    Now I still get a template bug (he gives me the object name instead of the values, but hey, without your help I still was searching how to reach those darn SampleData's.
    I also wonder why Microsoft didn't pay more attention to the Intellisence in Blend 4, as they did for Visual Studio 2010 Express ?

    Michael

    Monday, November 15, 2010 9:36 PM
  • Re the object name, the default .ToString() implementation inherited from object returns the classname. Guessing here, but you will need to bind to an appropriate property, or use a converter to present the info you want.
    Monday, November 15, 2010 9:44 PM
    Moderator