使用MultiValueConverter在WPF中更新Canvas上的Arc Segment Point

时间:2015-11-17 01:42:10

标签: c# wpf xaml multibinding imultivalueconverter

所以我正在完成一项任务,我们应该在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" 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();
    }
}

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

XAML:

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

C#MultiValueConverter:

int main()
{
    int n;
    int reverse = 0;    //initial value will be 0
    int temp;           //temporary variable

//Check if number is a palindrome===============================

    printf("Enter an integer: ");
    scanf_s("%d", &n);
    temp = n;           //Make input number have a temporary variable

    while (temp != 0)
    {
        reverse = reverse * 10;
        reverse = reverse + temp % 10;
        temp = temp / 10;
    }

//Check if number entered by user and it's reverse number is equal
if (reverse == n)
    printf("%d\t1, reverse is %d\n", n, reverse);    //Is a palindrome
else
    printf("%d\t0, reverse is %d\n", n, reverse);   //Not a palindrome
//==========================================================================

//Keep adding numbers until it reaches a palindrome
    int sum;

    while (n /= reverse)
    {
        sum = n + reverse;
        n++;
    }

if (reverse == sum)
    printf("%d it works", sum, reverse);
else
    ("%d didn't work", sum, reverse);

有人知道怎么做吗?当我打印出值时,MultiBind收到的所有内容都是正确的,我尝试返回新的Point()和格式化的字符串,但似乎没有任何效果。

更多信息:没有构建错误,我在Window.Resources标记中获得了MultiValueConverter,传递到多重绑定的所有值都来自滑块,当我返回时它们都是正确的使用与multibind相同的xaml代码的标签的字符串(如此):

{{1}}

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

1 个答案:

答案 0 :(得分:0)

好的,我已经弄清楚了;我制作了一个完整的MouthConverter:IMultiValueConverter并在转换器中创建了PathGeometry ,然后将其返回,链接到XAML和C#MultiBind here