简体   繁体   English

如何以编程方式设置ItemsSource属性?

[英]How to set ItemsSource property programmatically?

This is XAML code; 这是XAML代码;

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

How to assign this ItemSource attribute to the newTaskNameTextBox by C# programmatically ? 如何分配此ItemSource属性的newTaskNameTextBox通过C#编程?

(Solution for WPF) (WPF解决方案)

You should use the TryFindResource method. 您应该使用TryFindResource方法。

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

This searches up the logical tree, in the same way {StaticResource BankNamesList} does. 这以与{StaticResource BankNamesList}相同的方式搜索逻辑树。


UPDATE: (solution for WP8) 更新:(WP8的解决方案)

Sounds lile you're using WP8 (which doesn't include FindResource / TryFindResource ) so try this instead: 听起来您使用的是WP8 (不包括FindResource / TryFindResource ),所以请尝试以下操作:

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

UPDATE: (how to implement the missing TryFindResource) 更新:(如何实现缺少的TryFindResource)

Note that the code above requires the resource to exist in the owner of this code behind (eg the window). 请注意,上面的代码要求资源位于后面的代码所有者(例如,窗口)中。 However, there may be cases where the resource exists in another parent element up the logical tree. 但是,在某些情况下,资源可能存在于逻辑树上的另一个父元素中。 For example, you may be writing the code behind for a custom user control but the resource you're looking for exists in the MainWindow. 例如,您可能正在为自定义用户控件编写代码,但是要查找的资源存在于MainWindow中。 For such cases, it wouldn't be too hard to write a basic implementation of WPF's TryFindResouces , which has the advantage of searching up the logical tree ( source link ): 对于这种情况,编写WPF的TryFindResouces的基本实现TryFindResouces ,它具有搜索逻辑树( 源链接 )的优点

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

So if you include this FrameworkElementExtensions class in your namespace, then you should be able to do this (same code I've given for WPF originally): 因此,如果您在名称空间中包含此FrameworkElementExtensions类,那么您应该能够做到这一点(与我最初为WPF给出的代码相同):

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

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

newTaskNameTextBox.ItemsSource = Resources["BankNamesList"]

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM