简体   繁体   中英

How to set ItemsSource property programmatically?

This is XAML code;

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

How to assign this ItemSource attribute to the newTaskNameTextBox by C# programmatically ?

(Solution for WPF)

You should use the TryFindResource method.

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

This searches up the logical tree, in the same way {StaticResource BankNamesList} does.


UPDATE: (solution for WP8)

Sounds lile you're using WP8 (which doesn't include FindResource / TryFindResource ) so try this instead:

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

UPDATE: (how to implement the missing 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. 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 ):

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

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

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

newTaskNameTextBox.ItemsSource = Resources["BankNamesList"]

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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