使用MultiValueConverter更新WPF中画布上的弧段点

本文关键字:段点 MultiValueConverter 更新 WPF 使用 | 更新日期: 2023-09-27 18:22:14

所以我正在做一项任务,我们应该在C#和WPF中制作一个Mii Maker,我已经完成了所有工作,但问题是我们应该尽可能少地使用代码,而是使用绑定。我已经为其他一些部分创建了一个ValueConverter,它们工作得很好,但当我尝试使用MultiBind更新ArcSegment的点时(我需要一些值来保持一切居中),该点保持不变。

以下是没有多重绑定的工作代码:

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

我想删除该方法并将其提取到MultiValueConverter中(或两个,因为弧有两个点),并直接更新Point,而不是使用value_Changed方法,如下所示:

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#多值转换器:

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

有人知道怎么这样做吗?当我打印出值时,MultiBind会接收到所有正确的值,并且我尝试返回新的Point()和格式化的String,但似乎什么都不起作用。

更多信息:没有构建错误,我在Window.Resources标签中有MultiValueConverter,所有传递到多绑定的值都来自滑块,当我使用相同的多绑定xaml代码将字符串返回到标签时,它们都是正确的(就像这样):

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

我需要保留工作代码并处理它吗?或者有没有办法用MultiBind更新ArcSegment的点?

使用MultiValueConverter更新WPF中画布上的弧段点

好吧,我已经想好了;我制作了一个完整的MouthConverter:IMultiValueConverter,并在转换器内部创建了PathGeometry,然后返回它,链接到XAML和C#MultiBind此处的