How to set ItemsSource property programmatically?

2020-06-27 06:56发布

This is XAML code;

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

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

3条回答
欢心
2楼-- · 2020-06-27 07:21

Try this:

newTaskNameTextBox.ItemsSource = (IEnumerable)(Application.Current.Resources["BankNamesList"]);
查看更多
3楼-- · 2020-06-27 07:28

(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 (e.g. 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");
查看更多
家丑人穷心不美
4楼-- · 2020-06-27 07:42

If BankNamesList is resource in the resources of your window, then in code behind you can do:

newTaskNameTextBox.ItemsSource = Resources["BankNamesList"]
查看更多
登录 后发表回答