Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to bind a ListBox's ItemsSource to the results of a method within a static class.

Here is the XAML, using in the binding the class name, dot, then method name; I'm not at all sure this is supported, but I tried it:

HTML
<AppBarButton Icon="OpenFile" Label="Open Existing Photoset[s]" AutomationProperties.Name="Open File" >
    <Button.Flyout>
        <Flyout>
            <StackPanel Width="406">
                <ListBox x:Name="lstbxPhotosets" ItemsSource="{Binding PhotraxSQLiteUtils.GetPhotosets()}" FontSize="16" FontWeight="SemiLight" FontFamily="Segoe UI" Margin="0,0,0,10"  />
                <Button Content="Load" HorizontalAlignment="Right" FontSize="16" Margin="0,10,0,0" Tapped="btnOpenMap_Tapped" />
            </StackPanel>
        </Flyout>
    </Button.Flyout>
</AppBarButton>


...and here is the method:

C#
internal static List<String> GetPhotosets()
{
    List<String> psets = new List<string>();
    using (var db = new SQLite.SQLiteConnection(App.DBPath))
    {
        // Does SQLite support "DISTINCT"?
        string sql = "SELECT DISTINCT photosetName FROM PhotraxBaseData ORDER BY photosetName";
        db.Query<PhotraxBaseData>(sql);
    }
    return psets;
}


Apparently this way of doing it is unsupported, as I get nothing in my flyout's ListBox. So how can I assign the results of a query to a Listbox in a Windows Store App?
Posted
Comments
Sergey Alexandrovich Kryukov 29-Oct-14 2:26am    
How about writing some real code, instead of trying to stick everything in XAML?
And by the way, try to think why what you want is not supported. It can give you some understanding...
—SA
B. Clay Shannon 29-Oct-14 8:40am    
My understanding is that it is preferable to use binding where possible. If "real code" is the preferred method, how about an example?
Sergey Alexandrovich Kryukov 29-Oct-14 13:55pm    
Why "where possible"? Who told you that? You know there is a big party of "XAML purists". Nothing rational, just religion.

What example? Isn't it obvious?

To poll a database or some data source (GetPhotosets()), you should have some event and some code actually getting data. Now, what happens even of you could call GetPhotosets() from XAML. Theoretically, it could be possible. Now what? Let's see... Do you understand binding yourself? Binding says: every time X happens, do Y. What is X? It's always some dependency property. The property (any property) always have some side effect (setter and getter, even if one or both of them are empty). Therefore, programming system can use this side effect in the code generated in an agnostic way from XAML (do you understand that XAML is merely converted to the code by the build?) to respond according to binding.

In this case, respond to what? It just would say that the data for ListView is taken from GetPhotosets(). But at what moment of time? With "regular" binding, its clear: as soon as some properties are modified. What would it be in the case OP suggests? The behavior is simply undefined.

—SA
Sergey Alexandrovich Kryukov 29-Oct-14 13:57pm    
I put a short formal answer based on my comment above, please see.
—SA

According to MSDN[^], you'll need to use an ObjectDataProvider[^] as your binding source.

This blog post[^] has a nice example:
XML
<Window ... xmlns:local="clr-namespace:SimpleObjectDataProvider">
   <Window.Resources>
        <ObjectDataProvider 
            x:Key="colors"
            ObjectType="{x:Type local:ColorHelper}"
            MethodName="GetColorNames"
        />
    </Window.Resources>
    
    <StackPanel>
        <ComboBox 
            ItemsSource="{Binding Source={StaticResource colors}}"
        />
    </StackPanel>
</Window>


You might need to make your class public rather than internal; WPF doesn't seem to like working with internal classes from XAML.

EDIT: Sorry, just noticed you're using WinRT. It looks like the ObjectDataProvider isn't available on that platform.
 
Share this answer
 
v2
Comments
B. Clay Shannon 29-Oct-14 14:53pm    
Too bad it's not available on WinRT - that does look like an excellent solution! Thanks, anyway - maybe somebody else will find your answer and be able to use it.
Sergey Alexandrovich Kryukov 29-Oct-14 15:19pm    
5ed. Event though B. Clay Shannon says it's not available on WinRT, this is a good food for thought explaining the principle.
Never tried to work with WinRT, in part due to my negative views on Windows 8, but I have pretty bad impression...
—SA
B. Clay Shannon 29-Oct-14 15:21pm    
5ed.?
My answer is mostly written in the form of my comment to the comment by B. Clay Shannon, in comments to the question.
Basically, I focused not on what is actually implemented in XAML technology, but on the correctness of your idea.

I tried to explain how binding works, sorry if it is too short and not clear enough. There is a piece of explanation in my article, but basically, you can read on how dependency properties work. Their main purpose is the implementation of binding in XAML.

If you have some follow-up questions, I'll try to explain it in detail.

But basically, the simple solution would be: decide on what events your method GetPhotosets() should be called. Handle that event, call the method and then call some method used to populate your instance of ListView. You can formally do it by binding with some formal data source of course, to make it elegant and more isolated from UI. But then, you will have to populate this data source. Overall, this is pretty logical.

—SA
 
Share this answer
 
v2
Comments
B. Clay Shannon 29-Oct-14 14:21pm    
So I'm thinking maybe an OnCreated() (or similar) method of the flyout. I can't check now (not at a Windows 8 machine), but I hope there is some event like that where I can call the query. That would be the easiest way, I reckon. It doesn't need to be "two-way" - I'm only getting a List of String and displaying them, that's all.
Sergey Alexandrovich Kryukov 29-Oct-14 15:14pm    
Well, "some event" is the event to be handled. That is whole the point: "binding" a UIElement with method call would not bind anything.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900