错误 "Specified Visual is already a child of another Visual or the root of a CompositionTarget"

Error "Specified Visual is already a child of another Visual or the root of a CompositionTarget"

我正在开发一个控件,上面有一个 LiveChart CartesianChart 和一个列表框,我可以在其中选择显示的 LineSeries。我试图简化 following LiveCharts example

我的 xaml 看起来如下:

<Window x:Class="xxx.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:xxx"
    xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
    mc:Ignorable="d"
    Title="xxx" Height="460.36" Width="707.883">
<Grid ShowGridLines="True">
    <Grid.Resources>
        <local:ChannelReverseConverter x:Key="ChannelReverseConverter"></local:ChannelReverseConverter>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <!--Graph-->
        <RowDefinition Height="100*" />
        <!--EEPROM and misc-->
        <RowDefinition Height="20*"/>
        <!--Log window-->
        <RowDefinition Height="100"/>
    </Grid.RowDefinitions>

    <lvc:CartesianChart Series="{Binding ChartData}" LegendLocation="Right" DisableAnimations="True" Grid.Row="0">
        <lvc:CartesianChart.AxisY>
            <lvc:Axis IsMerged="True" Title="Data" LabelFormatter="{Binding YFormatter}"/>
        </lvc:CartesianChart.AxisY>
        <lvc:CartesianChart.AxisX>
            <lvc:Axis LabelFormatter="{Binding DateTimeFormatter}" />
        </lvc:CartesianChart.AxisX>
    </lvc:CartesianChart>


    <ListBox Name="ChannelSelect" Grid.Row="1" Background="Beige" ItemsSource="{Binding ChartData, Converter={StaticResource ChannelReverseConverter}}">

    </ListBox>


    <TextBox Grid.Row="2" Height="Auto" Background="AliceBlue" TextWrapping="Wrap" AcceptsReturn="True" Name="LogBox" VerticalScrollBarVisibility="Auto"/>


</Grid>

当我尝试 运行 时,出现上述错误。我想,在这种情况下唯一使用两次的 Visual 是 ChartData(它是一个 SeriesCollection)。 但我不明白:

问题不在于 ChartData(SeriesCollection 类型)。可能是ListBox使用不当。添加 ItemTemplate 似乎可以解决问题。这样:

    <ListBox Name="ChannelSelect" Grid.Row="1" Background="Beige" ItemsSource="{Binding ChartData, Converter={StaticResource ChannelReverseConverter}}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding (lvc:LineSeries.Title)}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>