是否可以绑定到XAML中的静态类中的方法? [英] Is it possible to bind to a method in a static class's in XAML?

查看:85
本文介绍了是否可以绑定到XAML中的静态类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将ListBox的ItemsSource绑定到静态类中方法的结果。



这是XAML,在绑定类名时使用,点,然后方法名称;我一点都不确定这是支持的,但我试过了:



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:

<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>





...以下是方法:< br $> b $ b



...and here is the method:

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;
}





显然这种方式不受支持,因为我的弹出式列表框中没有任何内容。那么如何将查询结果分配给Windows应用商店应用中的列表框?



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?

推荐答案

根据 MSDN [ ^ ],您需要使用 ObjectDataProvider [ ^ ]作为绑定源。



此博文 [ ^ ]有一个很好的例子:

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

This blog post[^] has a nice example:
<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>





您可能需要使你的班级公开而不是内部; WPF似乎不喜欢使用XAML的内部类。



编辑:对不起,刚刚注意到你正在使用WinRT。看起来 ObjectDataProvider 在该平台上不可用。



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

Sorry, just noticed you're using WinRT. It looks like the ObjectDataProvider isn't available on that platform.


我的回答主要是以我对该评论的形式写的。 B. Clay Shannon的评论,对这个问题的评论。

基本上,我不是关注XAML技术中实际实现的内容,而是关注你的想法的正确性。



我试图解释绑定是如何工作的,抱歉如果它太短而且不够清晰。我的文章中有一条解释,但基本上,您可以阅读依赖属性的工作原理。他们的主要目的是在XAML中实现绑定。



如果您有一些后续问题,我会尝试详细解释。



但基本上,简单的解决方案是:决定应该调用你的方法 GetPhotosets()的事件。处理该事件,调用该方法,然后调用一些用于填充 ListView 实例的方法。您当然可以通过绑定一些正式的数据源来正式执行此操作,以使其更优雅,更与UI隔离。但是,您将不得不填充此数据源。总的来说,这是非常符合逻辑的。



-SA
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


这篇关于是否可以绑定到XAML中的静态类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆