如何在 Stackpanel 的 Visual Studio 扩展中进行绑定?

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

我正在写一个 Visual Studio 扩展,它似乎不相关,但也许是。当调用 initializeComponent 时,我的代码在代码隐藏中抛出异常。我有一个 TeamExplorerSection,它创建一个 "model" 并将其传递给构造函数中的 SectionView(xaml)。它存储为视图的 属性。该模型有一些属性、名称、Id 等。我可以将这些属性绑定到视图,但它还有一个集合,我试图将其绑定到位于堆栈面板中的 ItemsControl。这是行不通的。

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

例外情况:

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.

我好像对这个绑定做错了什么。

XAML 将 DataTemplate 添加为项目,因此出现错误。您需要指定它是 ItemTemplate。试试这个。

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

区别在于DataTemplateItemTemplatesetter之内。