0

私が解決しようとしている問題はかなり簡単です。私は使用Microsoft.Phone.Controlsしていて、の2つのプロパティにバインドしようとしToggleSwitchているMainPageViewModelので、の状態をキャプチャし、ToggleSwitchその内容を「オン/オフ」から「たとえば、距離/時間」です。

私がやっていることはうまくいきません。それは、ドキュメントからはよくわからない慣習に関係しています(RTFM ...)。これは機能しません:

using System;
using System.Windows.Data;
using System.Collections.Generic;
using Caliburn.Micro;
using Microsoft.Phone.Controls;

public class AppBootstrapper : PhoneBootstrapper
{
    PhoneContainer container;

    protected override void Configure()
    {
        container = new PhoneContainer(RootFrame);

        container.RegisterPhoneServices();
        container.PerRequest<MainPageViewModel>();

        AddCustomConventions();
    }

    private static void AddCustomConventions()
    {
        ConventionManager.AddElementConvention<ToggleSwitch>(ToggleSwitch.IsCheckedProperty, "IsChecked", "Click")
            .ApplyBinding = (viewModelType, path, property, element, convention) =>
            {
                //Default binding to "IsChecked" property
                if (!ConventionManager.SetBinding(viewModelType, path + ".IsChecked", property, element, convention))
                    return false;

                if (!ConventionManager.HasBinding(element, ToggleSwitch.ContentProperty))
                {
                    var binding = new Binding(path + ".Content");

                    BindingOperations.SetBinding(element, ToggleSwitch.ContentProperty, binding);
                }

                return true;
            };
    }
}

    bool fixedDistance = true;
    public bool FixedDistance
    {
        get
        {
            return fixedDistance;
        }
        set
        {
            fixedDistance = value;
            NotifyOfPropertyChange(() => FixedDistance);

            if (fixedDistance)
            {
                FixedDistanceContent = "Distance";
            }
            else
            {
                FixedDistanceContent = "Time";
            }
        }
    }

    string fixedDistanceContent;

    public string FixedDistanceContent
    {
        get
        {
            return fixedDistanceContent;
        }
        set
        {
            fixedDistanceContent = value;
            NotifyOfPropertyChange(() => FixedDistanceContent);
        }
    }

ToggleSwitchは xaml がありますName=FixedDistance

私は素朴に (そして明らかに間違って)がプロパティにToggleSwitch.IsCheckedバインドされ、 が にバインドされることを期待しています。FixedDistanceToggleSwitch.ContentFixedDistanceContent

ありがとう!

4

1 に答える 1

1

次のようになります。

        ConventionManager.AddElementConvention<ToggleSwitch>(
            ToggleSwitch.IsCheckedProperty,
            "IsChecked",
            "Click");
            .ApplyBinding = (viewModelType, path, property, element, convention) =>
        {
            if (!ConventionManager.SetBinding(viewModelType, path, property, element, convention))
                return false;

            if (ConventionManager.HasBinding(element, ToggleSwitch.ContentProperty)) return true;

            var binding = new Binding(path + "Content") { Mode = BindingMode.TwoWay };
            BindingOperations.SetBinding(element, ToggleSwitch.ContentProperty, binding);

            return true;
        };

トリックを行います。

于 2011-11-19T17:38:08.280 に答える