【问题标题】:How to set ItemsSource property programmatically?如何以编程方式设置 ItemsSource 属性?
【发布时间】:2013-09-07 18:41:02
【问题描述】:

这是 XAML 代码;

<toolkit:AutoCompleteBox x:Name="newTaskNameTextBox"
                         ItemsSource="{StaticResource BankNamesList}" />

如何通过 C# 以编程方式将此 ItemSource 属性分配给 newTaskNameTextBox

【问题讨论】:

    标签: c# xaml windows-phone-8 windows-phone


    【解决方案1】:

    (WPF 解决方案)

    您应该使用TryFindResource 方法。

    newTaskNameTextBox.ItemsSource = 
        (IEnumerable)newTaskNameTextBox.TryFindResource("BankNamesList");
    

    这会以与{StaticResource BankNamesList} 相同的方式搜索逻辑树。


    更新:(WP8 的解决方案)

    听起来你正在使用 WP8(不包括 FindResource / TryFindResource)所以试试这个吧:

    newTaskNameTextBox.ItemsSource = (IEnumerable)Resources["BankNamesList"];
    

    更新:(如何实现缺少的 TryFindResource)

    请注意,上面的代码要求资源存在于此代码的所有者中(例如窗口)。但是,可能存在资源存在于逻辑树上的另一个父元素中的情况。例如,您可能正在为自定义用户控件编写代码,但您要查找的资源存在于 MainWindow 中。对于这种情况,编写 WPF 的 TryFindResouces 的基本实现并不难,它具有搜索逻辑树 (source link) 的优势

    public static class FrameworkElementExtensions
    {
        public static object TryFindResource(this FrameworkElement element, object resourceKey)
        {
            var currentElement = element;
    
            while (currentElement != null)
            {
                var resource = currentElement.Resources[resourceKey];
                if (resource != null)
                {
                    return resource;
                }
    
                currentElement = currentElement.Parent as FrameworkElement;
            }
    
            return Application.Current.Resources[resourceKey];
        }
    }
    
    /**********************************************************************/
    // Or, the recursive version of TryFindResource method as suggested by @Default:
    
    public static object TryFindResource(this FrameworkElement element, object resourceKey)
    {
        if (element == null)
            return Application.Current.Resources[resourceKey];
    
        var resource = element.Resources[resourceKey];
        if (resource != null)
        {
            return resource;
        }
        return TryFindResource(element.Parent, resourceKey);
    }
    

    因此,如果您在命名空间中包含此 FrameworkElementExtensions 类,那么您应该能够做到这一点(我最初为 WPF 提供的代码相同):

    newTaskNameTextBox.ItemsSource = 
        (IEnumerable)newTaskNameTextBox.TryFindResource("BankNamesList");
    

    【讨论】:

    • 错误'Microsoft.Phone.Controls.AutoCompleteBox' does not contain a definition for 'TryFindResource' and no extension method 'TryFindResource' accepting a first argument of type 'Microsoft.Phone.Controls.AutoCompleteBox' could be found (are you missing a using directive or an assembly reference?)
    • @Csharp_Newbie 尝试编辑后的代码。您的问题标签包括wpfwindows-phone,因此您必须使用windows-phone...
    • 其他来到这里的用户可能正在寻找 WPF 解决方案(即使现在已删除该标签..)。是否也可以保留该解决方案?我认为这将使未来的访客受益。
    • 我冒昧地添加了TryFindResource 的递归版本作为对您答案的评论(因此目前它不可见)。对我来说,它更短且嵌套更少,但仍然易于阅读。作为对我的回答无效,并且渴望发表评论。我希望你不介意..
    【解决方案2】:

    如果 BankNamesList 是您窗口资源中的资源,那么您可以在后面的代码中执行以下操作:

    newTaskNameTextBox.ItemsSource = Resources["BankNamesList"]
    

    【讨论】:

    • 错误:Cannot implicitly convert type 'object' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
    • 将资源类型转换为 (IEnumerable)Resources["BankNamesList"]
    【解决方案3】:

    试试这个:

    newTaskNameTextBox.ItemsSource = (IEnumerable)(Application.Current.Resources["BankNamesList"]);
    

    【讨论】:

    • 错误:Cannot implicitly convert type 'object' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
    猜你喜欢
    • 2021-06-22
    • 2023-01-03
    • 2021-05-20
    • 2012-08-20
    相关资源
    最近更新 更多