【发布时间】:2016-07-14 16:55:14
【问题描述】:
我正在实现一个 wpf 控件,它提供了 xaml 和在 Web 浏览器中运行的 Google 地图之间的一些通用绑定。目前,绑定是单向的并且工作正常。
接下来我需要做的是在指定双向绑定时将值写回视图模型。我开始的属性是谷歌地图的缩放。当它在浏览器中更改时,我可以在页面上运行 js,以新的缩放级别回调我的 C# 代码。
如果选择双向绑定,向 DP 提供这个新值的正确方法是什么,例如它会更新视图模型的缩放级别?
我当前的缩放级别 DP 代码:
#region ZoomProperty
//Called from the web page
private JSValue MapZoom_OnMapZoomChanged(JSValue[] arguments) {
string zoom = arguments[0];
//where do I set the zoom so that the view model bound property is updated?
return null;
}
public static readonly DependencyProperty ZoomProperty =
DependencyProperty.Register("Zoom", typeof(string), typeof(GoogleMap), new FrameworkPropertyMetadata(string.Empty, OnZoomPropertyChanged), OnZoomPropertyValidate);
public string Zoom {
get { return (string)GetValue(ZoomProperty); }
set { SetValue(ZoomProperty, value); }
}
private static bool OnZoomPropertyValidate(object value) {
return value is string;
}
private static void OnZoomPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) {
GoogleMap control = source as GoogleMap;
control.SetZoom(e.NewValue.ToString());
}
private string zoom;
public void SetZoom(string value) {
if (!googleMapPageReady) {
zoom = value;
return;
}
webControl.ExecuteJavascript(string.Format("setZoom({0})", zoom));
}
#endregion
【问题讨论】:
-
请注意,您还可以使用 Register 方法的另一个重载设置
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault标志,以使依赖项属性默认为双向绑定。
标签: c# wpf xaml dependency-properties