使用 Caliburn (micro) 绑定到两个或多个控件属性

发布于 2024-12-15 20:33:26 字数 2629 浏览 6 评论 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

谢谢!

The problem I'm trying to solve is fairly straight forward, I'm using Microsoft.Phone.Controls and I'm trying to bind to two properties of ToggleSwitch in my MainPageViewModel so I can capture the state of the ToggleSwitch and change its content from 'on/off' to 'distance/time' for example.

What I'm doing doesn't work, it's something to do with conventions that I haven't quite figured out from the docs (RTFM...). This doesn't work:

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

and

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

where the ToggleSwitch has the xaml Name=FixedDistance.

I'm naively (and clearly incorrectly) expecting the ToggleSwitch.IsChecked to be bound to the FixedDistance property, and the ToggleSwitch.Content to be bound to FixedDistanceContent.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2024-12-22 20:33:26

看起来像这样:

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

成功了。

It looks like this:

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

does the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文