0

我要解决的问题相当简单,我正在使用Microsoft.Phone.Controls并且我正在尝试绑定到我的两个属性,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被绑定到该FixedDistance属性,并且ToggleSwitch.Content被绑定到FixedDistanceContent.

谢谢!

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 回答