我有一系列函数,想要在画布上绘制它们。所以我认为绘制这些函数的正确数据类型是 Polyline
。
因此,我需要一个转换器将这些函数转换为折线集合,最后在画布上显示它们。
以下是XAML代码。
<ItemsControl ItemsSource="{Binding WaveCollection,
RelativeSource ={RelativeSource FindAncestor, AncestorType={x:Type Window}},
Converter={StaticResource PlotterConverter}}" Margin="10,10,0,239" HorizontalAlignment="Left" Width="330">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="GhostWhite" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
这里是将波形转换为折线的部分转换器。绑定模式为单向。
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var collection = value as IEnumerable<Waveform>;
return new ObservableCollection<Polyline>(GetPlots(collection));
}
然而,当我设置断点时,我注意到它仅在程序开始时触发一次。此时集合为空,因此不会发生任何特殊情况,但在此之后,当我向集合中添加项目时,没有发生任何事情。没有事件被触发。为什么?
为了确认,我还将这段代码添加到转换器中,以查看是否真的触发,但没有任何反应。
var collection = value as ObservableCollection<Waveform>;
collection.Clear(); // this is supposed to clear collection. if binding works correct!
//...
请注意,我还将此集合绑定到
listview
中,以显示波浪信息,当集合更新时,这也很好地工作。编辑:
我猜问题在于这部分代码
return new ObservableCollection<Polyline>...
,它会在第一次运行时更改集合,并且会搞乱绑定?