使用 Caliburn (micro) 绑定到两个或多个控件属性
我试图解决的问题相当简单,我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来像这样:
成功了。
It looks like this:
does the trick.