-2

So I'm working on an assignment where we're supposed to make a Mii Maker in C# and WPF, and I've got everything working but the caviat is that we're supposed to use as little code as possible, and use binding instead. I've created a ValueConverter for a few of the other parts, and they worked fine, but when I try to update the point of an ArcSegment using a MultiBind (I need a few values to keep everything centered) the point stays stationary.

Here's the working code without the multi-bind:

XAML:

<Path Stroke="Black" StrokeThickness="3">
     <Path.Data>
          <PathGeometry>
               <PathGeometry.Figures>
                    <PathFigure x:Name="LeftMouthCorner" StartPoint="221,190" IsClosed="False">
                         <ArcSegment x:Name="RightMouthCorner" Point="291,190" Size="{Binding ElementName=MouthHappinessSlider, Path=Value, Converter={StaticResource HappinessConverter}}">
                           </ArcSegment>
                       </PathFigure>
                   </PathGeometry.Figures>
               </PathGeometry>
           </Path.Data>
      </Path>

C#:

    private void MouthWidthSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        MouthWidthLabel.Content = String.Format("Mouth Width: {0}", MouthWidthSlider.Value);
        if (MouthWidthSlider != null && MouthHeightSlider != null && HeadYPosSlider != null)
        {
            LeftMouthCorner.StartPoint = new Point(((myCanvas.ActualWidth / 2) - MouthWidthSlider.Value), (HeadYPosSlider.Value + MouthHeightSlider.Value));
            RightMouthCorner.Point = new Point(((myCanvas.ActualWidth / 2) + MouthWidthSlider.Value), (HeadYPosSlider.Value + MouthHeightSlider.Value));
        }
    }

I want to remove that method and extract it into a MultiValueConverter (or two, since the arc has two points), and update the Point directly instead of using the value_Changed method, like so:

XAML:

            <Path Stroke="Black" StrokeThickness="3">
                <Path.Data>
                    <PathGeometry>
                        <PathGeometry.Figures>
                            <PathFigure x:Name="LeftMouthCorner" StartPoint="221,190" IsClosed="False">
                                <ArcSegment x:Name="RightMouthCorner" Size="{Binding ElementName=MouthHappinessSlider, Path=Value, Converter={StaticResource HappinessConverter}}">
                                    <ArcSegment.Point>

                                        <MultiBinding Converter="{StaticResource RMCConverter}" UpdateSourceTrigger="PropertyChanged">

                                            <Binding ElementName="HeadYPosSlider" Path="Value" />
                                            <Binding ElementName="MouthHeightSlider" Path="Value" />
                                            <Binding ElementName="MouthWidthSlider" Path="Value" />
                                            <Binding ElementName="myCanvas" Path="Width" />

                                        </MultiBinding>

                                    </ArcSegment.Point>
                                </ArcSegment>
                            </PathFigure>
                        </PathGeometry.Figures>
                    </PathGeometry>
                </Path.Data>
            </Path>

C# MultiValueConverter:

public class RMCConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        //HYP / MHS / MWS / mCW
        var hyp = values[0];
        var mhs = values[1];
        var mws = values[2];
        var mcw = values[3];
        double HeadYPos = (double)hyp,
            MouthHeight = (double)mhs,
            MouthWidth = (double)mws,
            CanvasWidth = (double)mcw;
        return new Point(((CanvasWidth / 2) + MouthWidth), (HeadYPos + MouthHeight));
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Does anybody know how to do it this way? When I print out the values the MultiBind receives everything's correct, and I've tried returning both the new Point() and a formatted String, but nothing seems to work.

Further information: There are no build errors, I've got the MultiValueConverter in the Window.Resources tag, all the values getting passed into the multibind are from sliders, and they're all correct when I returned the string to a label using the same xaml code for the multibind (like so):

 <Label>
     <Content>
         <MultiBind-Code from above goes here>
     </Content>
 </Label>

Do I need to keep the working code and just deal with it, or is there a way to update the ArcSegment's point with the MultiBind?

Tenurian
  • 1
  • 2
  • Okay, so I've figured it out; I made a full MouthConverter and created the PathGeometry inside the converter, then returned it, link to XAML and C# MultiBind below: [link](http://pastebin.com/6zDTaXjd) – Tenurian Nov 17 '15 at 02:09
  • The `MultiValueConverter` should work. Without [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that reliably reproduces the problem, it's not possible to help you fix it though. I _would_ recommend not using `UpdateSourceTrigger`; in fact, the binding should just be `Mode="OneWay"`, since you can't convert from target to source. Don't forget that binding errors are written to debug output; there is often useful information there when you can't figure out why a binding doesn't work. – Peter Duniho Nov 17 '15 at 05:01

1 Answers1

0

Okay, so I've figured it out; I made a full MouthConverter : IMultiValueConverter and created the PathGeometry inside the converter, then returned it, link to XAML and C# MultiBind here

Tenurian
  • 1
  • 2