How do I do binding in a Visual Studio extension a Stackpanel?

0

I am writing a Visual Studio extension, which does not seem relevant, but perhaps it is. My code is throwing an exception in the codebehind when the initializeComponent is being called. I have a TeamExplorerSection which creates a "model" and passes it to the SectionView (the xaml) in the constructor. It is stored as a property of the view. The model has some properties, Name, Id etc. I am able to bind these properties to the view, but it also has a collection which I am trying to bind to an ItemsControl sitting in a stackpanel. This does not work.

<UserControl x:Class="ReviewPlus.ReviewPlusTeamExplorerSectionView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:ReviewPlus.ViewModels="clr-namespace:ReviewPlus.ViewModels"
         mc:Ignorable="d" 
         d:DesignHeight="250" d:DesignWidth="300">
<UserControl.DataContext>
    <ReviewPlus.ViewModels:RelatedReviewsViewModel/>
</UserControl.DataContext>    
<StackPanel>
    <ItemsControl Name="RelatedReviewsICtl" ItemsSource="{Binding RelatedReviews}">
        <DataTemplate>
            <StackPanel  Orientation="Horizontal" HorizontalAlignment="Left">                 
                <TextBlock Text="{Binding Path=Name}" />
                <TextBlock Text="{Binding Path=PreviousId}"/>
                <TextBlock Text="{Binding Path=Id}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl>    
</StackPanel>

Here is the exception:

System.Windows.Markup.XamlParseException occurred HResult=0x80131501 Message='Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.' Line number '22' and line position '19'. Source=PresentationFramework StackTrace: at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at ReviewPlus.TeamExplorerSectionView.InitializeComponent() in C:\Dev\ReviewPlus\ReviewPlus\CodePlusTeamExplorerSectionView.xaml:line 1 Inner Exception 1: InvalidOperationException: Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

I seem to be doing something wrong with this binding.

wpf
xaml
visual-studio-2017
xaml-binding
asked on Stack Overflow Aug 11, 2017 by Noel • edited Aug 17, 2017 by Hugo Quintela Ribeiro

1 Answer

0

The XAML is adding DataTemplateas an item, hence the error. You need to specify that it's the ItemTemplate. Try this.

<ItemsControl Name="RelatedReviewsICtl" ItemsSource="{Binding RelatedReviews}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel  Orientation="Horizontal" HorizontalAlignment="Left">                 
                <TextBlock Text="{Binding Path=Name}" />
                <TextBlock Text="{Binding Path=PreviousId}"/>
                <TextBlock Text="{Binding Path=Id}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The difference is that DataTemplate is within the ItemTemplatesetter.

answered on Stack Overflow Aug 11, 2017 by Jason Tyler

User contributions licensed under CC BY-SA 3.0