Why is my namespace not recognized in Visual Studio / xaml

these are my 2 classes a Attachable Property SelectedItems:

code is from here: Sync SelectedItems in a muliselect listbox with a collection in ViewModel

The namespace TBM.Helper is for sure proper as it works for other classes too.

The namespace reference is also in the xaml file AND the SelectedItems.cs file!

xmlns:Helper="clr_namespace:TBM.Helper"

But <ListBox Helper:SelectedItems.Items="{Binding SelectedItems}" ...

does not work because =>

The property 'SelectedItems.Items' does not exist in XML namespace 'clr_namespace:TBM.Helper'. The attachable property 'Items' was not found in type 'SelectedItems

What do I have to change ?

    using System;

using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Collections; using System.Windows;

namespace TBM.Helper { public static class SelectedItems : DependencyObject { private static readonly DependencyProperty SelectedItemsBehaviorProperty = DependencyProperty.RegisterAttached( "SelectedItemsBehavior", typeof(SelectedItemsBehavior), typeof(ListBox), null);

    public static readonly DependencyProperty ItemsProperty = DependencyProperty.RegisterAttached(
            "Items",
            typeof(IList),
            typeof(SelectedItems),
            new PropertyMetadata(null, ItemsPropertyChanged));

    public static void SetItems(ListBox listBox, IList list) { listBox.SetValue(ItemsProperty, list); }
    public static IList GetItems(ListBox listBox) { return listBox.GetValue(ItemsProperty) as IList; }

    private static void ItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var target = d as ListBox;
        if (target != null)
        {
            GetOrCreateBehavior(target, e.NewValue as IList);
        }
    }

    private static SelectedItemsBehavior GetOrCreateBehavior(ListBox target, IList list)
    {
        var behavior = target.GetValue(SelectedItemsBehaviorProperty) as SelectedItemsBehavior;
        if (behavior == null)
        {
            behavior = new SelectedItemsBehavior(target, list);
            target.SetValue(SelectedItemsBehaviorProperty, behavior);
        }

        return behavior;
    }
}

}

using System.Windows;

using System.Windows.Controls; using System.Collections;

namespace TBM.Helper { public class SelectedItemsBehavior { private readonly ListBox _listBox; private readonly IList _boundList;

    public SelectedItemsBehavior(ListBox listBox, IList boundList)
    {
        _boundList = boundList;
        _listBox = listBox;

        SetSelectedItems();

        _listBox.SelectionChanged += OnSelectionChanged;
        _listBox.DataContextChanged += OnDataContextChanged;
    }

    private void SetSelectedItems()
    {
        _listBox.SelectedItems.Clear();

        foreach (object item in _boundList)
        {
            // References in _boundList might not be the same as in _listBox.Items
            int i = _listBox.Items.IndexOf(item);
            if (i >= 0)
                _listBox.SelectedItems.Add(_listBox.Items[i]);
        }
    }

    private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        SetSelectedItems();
    }

    private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        _boundList.Clear();

        foreach (var item in _listBox.SelectedItems)          
            _boundList.Add(item);           
    }
}

}


At first I thought it is a typo (and maybe it is), but since your exception also says clr_namespace:TBM.Helper with an underscore, I think it might actually be in your code. Try to replace the underscore with a hyphen like this:

xmlns:Helper="clr-namespace:TBM.Helper"

I did not look through all the other code, so maybe there is another problem. However, you should try the above first. Good luck!

EDIT: Is your SelectedItemsBehavior class in the same assembly as your XAML? Otherwise, you will have to reference the other assembly in your project and change your namespace declaration to something like this:

xmlns:Helper="clr-namespace:TBM.Helper;assembly=MyOtherAssemblyName"

Furthermore, you cannot set a base class for a static class:

public static class SelectedItems : DependencyObject

This is not possible. Simply remove the : DependencyObject

If this still does not help, I am out of here. Your code is really messy, at least how you posted it...

链接地址: http://www.djcxy.com/p/53886.html

上一篇: 简单的方法将Iterable更改为Collection

下一篇: 为什么我的命名空间在Visual Studio / xaml中无法识别