私が解決しようとしている問題はかなり簡単です。私は使用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
バインドされ、 が にバインドされることを期待しています。FixedDistance
ToggleSwitch.Content
FixedDistanceContent
ありがとう!