Question

I have successfully bound a Polygon's Points using a IValueConverter. Now I need to use a IMultiValueConverter.

Let me outline what I am trying to accomplish first, if anyone sees a better way, tell me! I can use .NET 4.5 fyi.

I have a data store with a bunch of objects in it. These objects do not have any type of Point/PointCollection associated with it. I am able to create Points based on properties of the object in the data store, if I have the parents Height. Without the Height I can't generate points.

I currently have a working implementation, but I preform all the Point calculations in code. This leads to performance issues because I have to re-calculate all objects every time the parents Height changes.

On to the code:

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

And my dumb converter code:

    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(); }
}

BTW, here is the error I am getting:

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')
Was it helpful?

Solution

lol, well I guess I didn't exhaust my debugging.

I needed to pass back a real PointCollection, not a string like I had done with a regular IValueConverter.

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var pc = new PointCollection();
        pc.Add(new Point(10, 255));
        pc.Add(new Point(500, 255));
        pc.Add(new Point(500, 200));
        pc.Add(new Point(400, 150));
        pc.Add(new Point(200, 150));
        pc.Add(new Point(10, 200));
        return pc;//"10,255 500,255 500,200 400,150 200,150 10,200";
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top