views:

359

answers:

2

Sorry for a very specific question, bot why VS2008 and VS2010 crashes with this code? I though it's a common to use ObjectDataProvider to bind property. I seen examples in many places on the web, but werether I try to use it I get exception and my VS closes.

So, what is wrong with my code? Should I register a bug for VS?

<Window x:Class="ShortcutsManagementAddin.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ShortcutsManagementAddin"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider x:Key="Window1" ObjectType="{x:Type local:Window1}" />
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="30" />
        </Grid.ColumnDefinitions>


        <TreeView ItemsSource="{Binding Source={StaticResource Window1}, Path=Categories}"></TreeView>
    </Grid>
</Window>

namespace ShortcutsManagementAddin
{
    public class Shortcut
    {
        public string CategoryName;
    }

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private List<Shortcut> categories = new List<Shortcut>();
        public List<Shortcut> Categories
        {
            get { return categories; }
        }

        public Window1()
        {
            categories.Add(new Shortcut { CategoryName = "Category 1" });
            categories.Add(new Shortcut { CategoryName = "Category 2" });
            categories.Add(new Shortcut { CategoryName = "Category 3" });
            categories.Add(new Shortcut { CategoryName = "Category 4" });
            categories.Add(new Shortcut { CategoryName = "Category 5" });

            InitializeComponent();
        }
    }
}
A: 

From your code sample it looks like you're trying to create an instance of Window1 from within the XAML of Window1. Given that would result in an infinite loop, I could see that crashing the app or Visual Studio.

If you want to bind the members of the collection to the TreeView, it would be better to create a separate class (which will contain the list), and set an instance of that class as the DataContext of the window. You can then bind the ItemsSource of the TreeView to a member of that class by specifying the property name as the Path.

Andy
Thanks, I haven't realized that there was new object created.
Sergej Andrejev
A: 

Confirming. Same crash here on Visual Studio 2010 RTM. I also tried to use Window1. Thing is, sometimes the designer displayed the binding without any problem.

Seems like there must be a way of using Window1.

BSalita