我已经使用 IValueConverter 成功绑定了多边形的点。现在我需要使用 IMultiValueConverter。
让我先概述一下我要完成的工作,如果有人看到更好的方法,请告诉我!我可以使用 .NET 4.5 仅供参考。
我有一个包含一堆对象的数据存储。这些对象没有与之关联的任何类型的 Point/PointCollection。如果我有父母高度,我可以根据数据存储中对象的属性创建点。没有高度,我无法生成点。
我目前有一个有效的实现,但我在代码中执行了所有点计算。这会导致性能问题,因为每次父母高度发生变化时我都必须重新计算所有对象。
上代码:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Converters="clr-namespace:Converters"
x:Class="MainWindow"
Title="MainWindow" Height="611" Width="525">
<Window.Resources>
<Converters:PointsConverter x:Key="PointsConverter" />
</Window.Resources>
<Grid>
<Slider x:Name="slider" HorizontalAlignment="Left" Margin="344,10,0,0" VerticalAlignment="Top" Width="114" Value="1" Minimum="0.1" Maximum="4"/>
<ScrollViewer x:Name="TimelineScroller" ClipToBounds="True" VerticalScrollBarVisibility="Disabled"
Focusable="False" HorizontalScrollBarVisibility="Visible"
Visibility="Visible" Margin="50,93,35,206">
<Grid ClipToBounds="True">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<ItemsControl x:Name="TopItemControl" Grid.Row="1" ItemsSource="{Binding TopData}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas x:Name="TopAxis" Background="#FF65656C" Grid.Row="1" RenderTransformOrigin="0.5,0.5">
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="{Binding Value, ElementName=slider}"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Canvas.RenderTransform>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl >
<Polygon x:Name="polygon" Stroke="Black" RenderTransformOrigin="0.5,0.5">
<Polygon.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="{Binding Value, ElementName=slider}"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Polygon.RenderTransform>
<Polygon.Points>
<MultiBinding Converter="{StaticResource PointsConverter}">
<Binding ElementName="TopAxis" Path="ActualHeight"/>
<Binding />
</MultiBinding>
</Polygon.Points>
</Polygon>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</ScrollViewer>
</Grid>
还有我的哑转换器代码:
public class PointsConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//simple string for testing
return "10,255 500,255 500,200 400,150 200,150 10,200";
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
}
顺便说一句,这是我得到的错误:
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='10,255 500,255 500,200 400,150 200,150 10,200' MultiBindingExpression:target element is 'Polygon' (Name='polygon'); target property is 'Points' (type 'PointCollection')