Multibinding for Polyline points doesnt reflect

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pals28
    New Member
    • Dec 2012
    • 1

    Multibinding for Polyline points doesnt reflect

    Well i am providing you code for a sample program. Here i have a line which needs to have an arrow head so i have visual brush which contains a line and a polyline which draws the arrow head.I have two converters named StrokeConverter and ArrowConverter which is used to provide the stroke thickness and draw the arrow respectively. I have four textbox which are binded to the lines x1,y1,x2,y2 four changing the lines position on runtime as i need to provide user the flexibility to rotate and reposition the line.

    Code:
    Windows.xaml:-
    
    <Window x:Class="Marker_new.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:my="clr-namespace:Marker_new"
            Title="MainWindow" Height="550" Width="525">
            
        <Window.Resources>
            <my:ArrowConverter x:Key="ArrowHead"/>
            <my:StrokeConverter x:Key="Stroke"/>
        </Window.Resources>
        <Canvas Background="AliceBlue">
            <Line x:Name="arrowLine" X1="-100" Y1="0" X2="10" Y2="200"  StrokeThickness="20" Canvas.Left="253" Canvas.Top="48" >
                <Line.Stroke>
                    <VisualBrush Stretch="None" >
                        <VisualBrush.Visual>
                            <Canvas>
                                <Line x:Name="Line_Arrow" Stroke="Gray"  StrokeThickness="{Binding StrokeThickness,RelativeSource={RelativeSource FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}},Converter={StaticResource Stroke}, ConverterParameter=Line_Arrow}" X1="{Binding X1,RelativeSource={RelativeSource FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}}}" X2="{Binding X2,RelativeSource={RelativeSource FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}}}" Y1="{Binding Y1,RelativeSource={RelativeSource FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}}}" Y2="{Binding Y2,RelativeSource={RelativeSource FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}}}"  />
                                <Polyline x:Name="ArrowStart"  Stroke="Black" StrokeThickness="{Binding ElementName=Line_Arrow,Path=StrokeThickness}" >
                                    <Polyline.Points>
                                        <MultiBinding Converter="{StaticResource ArrowHead}" ConverterParameter="ArrowStart" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
                                            <Binding Path="StrokeThickness" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}}" />
                                            <Binding Path="X1" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}}" />
                                            <Binding Path="X2" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}}"/>
                                            <Binding Path="Y1" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}}"/>
                                            <Binding Path="Y2" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Line}}"/>
                                        </MultiBinding>
                                    </Polyline.Points>
                                    
                                </Polyline>
                                
                            </Canvas>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Line.Stroke>
            </Line>
            <Polyline Points="16.5882797876383,13.3816384544783 15,0 28.2174998970398,2.62460985328183" Stroke="Red" StrokeThickness="2" Canvas.Left="44" Canvas.Top="35" />
            <TextBox Text="{Binding ElementName=arrowLine,Path=X1}" Canvas.Left="310" Canvas.Top="12" Height="28" Name="textBox1" Width="57" />
            <TextBox Text="{Binding ElementName=arrowLine,Path=Y1, Mode=TwoWay}" Canvas.Left="434" Canvas.Top="12" Height="28" Name="textBox2" Width="57" />
            <TextBox Text="{Binding ElementName=arrowLine,Path=X2,Mode=TwoWay}" Canvas.Left="310" Canvas.Top="70" Height="28" Name="textBox3" Width="57" />
            <TextBox Text="{Binding ElementName=arrowLine,Path=Y2,Mode=TwoWay}" Canvas.Left="434" Canvas.Top="70" Height="28" Name="textBox4" Width="57" />
            <Label Canvas.Left="253" Canvas.Top="12" Content="X1" Height="28" Name="label1" />
            <Label Canvas.Left="382" Canvas.Top="12" Content="Y1" Height="28" Name="label2" />
            <Label Canvas.Left="253" Canvas.Top="68" Content="X2" Height="28" Name="label3" />
            <Label Canvas.Left="382" Canvas.Top="68" Content="Y2" Height="28" Name="label4" />
        </Canvas>
    </Window>
    StrokeConverter.cs:-
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Data;
    
    namespace Marker_new
    {
        class StrokeConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                switch ((string)parameter)
                {
                    case "Line_Square":
                    case "Line_Ellipse":
                        return ((double)value - (double)1) / 3;
                        break;
                    case "Line_Triangle":
                        return ((((double)value) / (2 * Math.Sin(Math.PI / 5)) - (double)1)) / 3;
                    case "Line_Arrow":
                        
                        return ((double)value - 2 * Math.Sin(Math.PI / 5)) / (6 * Math.Sin(Math.PI / 5) + (double)1);
                        break;
                    default:
                        return null;
                }
    
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    ArrowConverter.cs:-
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows;
    
    namespace Marker_new
    {
        public class ArrowConverter : IMultiValueConverter
        {
            PointCollection _ptCollectionsStart = new PointCollection();
            PointCollection _ptCollectionsEnd = new PointCollection();
            Point pt1;
            Point pt2;
            public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                
                    double _strokethickness;
                    _strokethickness = ((double)values[0] - 2 * Math.Sin(Math.PI / 5)) / (6 * Math.Sin(Math.PI / 5) + (double)1);
                    double _triangleLength = 3 * _strokethickness + (double)1;
                    pt1.X = (double)values[1];
                    pt1.Y = (double)values[3];
                    pt2.X = (double)values[2];
                    pt2.Y = (double)values[4];
                    switch ((string)parameter)
                    {
                        case "ArrowStart":
                            _ptCollectionsStart.Clear();
                            double ArrowLength = 3 * _strokethickness + 1;
                            Matrix matx = new Matrix();
                            Vector vect = pt2 - pt1;
                            vect.Normalize();
                            vect *= ArrowLength;
                            matx.Rotate(36);
                            _ptCollectionsStart.Add(pt1  + vect * matx);
                            _ptCollectionsStart.Add(pt1);
                            matx.Rotate(-72);
                            _ptCollectionsStart.Add(pt1 + vect * matx);
                            return _ptCollectionsStart;
                            break;
    
                        //case "ArrowEnd":
    // not implemented so far
                        //    break;
                        default:
                            return 10;
                    }
               
    
            }
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }=


    For the first tym its is coming fine but when i change the value of line points using textbox at runtime the arrowconverter is getting called and its providing changed values for arrow to be drawn but its not getting reflected. Let me know whats the issue.
    Last edited by Rabbit; Dec 23 '12, 04:41 AM. Reason: Please use code tags when posting code.
Working...