2016-07-13 12 views
0

を継承したクラスのプロパティを依存性結合、私はXAMLの宣言は次のように起動する「GoogleMap.xaml/.csファイル」という名前のWPFのユーザーコントロールを持っています背後にあるコード:WPF:</p> <pre><code><controls:BaseUserControl x:Class="CompanyNamespace.Controls.GoogleMap" </code></pre> <p>:ユーザーコントロールの基本クラス

public partial class GoogleMap : BaseUserControl{ 

     public static readonly DependencyProperty MapCenterProperty = 
      DependencyProperty.Register("MapCenter", typeof(string), typeof(GoogleMap), new FrameworkPropertyMetadata(string.Empty, OnMapCenterPropertyChanged)); 

    public string MapCenter { 
     get { return (string)GetValue(MapCenterProperty); } 
     set { SetValue(MapCenterProperty, value); } 
    } 

    private static void OnMapCenterPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e){ 
     GoogleMap control = source as GoogleMap; 
     control.SetCenter(e.NewValue.ToString()); 
    } 
... 
} 

私はGoogleMap.xamlからプロパティに対処できるようにするための正しい構文は何:

MapCenter="{Binding Location}" 

プロパティがBaseUserControlクラスにある場合、GoogleMap.xamlからこのプロパティにのみバインドできます。

更新 GoogleMapViewModel.cs

public class GoogleMapViewModel : ContainerViewModel{ 


    private string location; 
    [XmlAttribute("location")] 
    public string Location { 
     get { 
      if (string.IsNullOrEmpty(location)) 
       return appContext.DeviceGeolocation.Location(); 

      return ExpressionEvaluator.Evaluate(location); 
     } 
     set { 
      if (location == value) 
       return; 

      location = value; 
      RaisePropertyChanged("Location"); 
     } 
    } 
+1

。あなたの 'DataContext'はどのように見えますか? –

+0

私はカスタムコントロールを作成し、その視覚を 'ControlTemplate'で定義する方が良いと思います。 –

+0

にvmが追加されました – jchristof

答えて

1

独自のXAMLでユーザーコントロールのプロパティをバインドするために、あなたはスタイル宣言することができます:

<controls:BaseUserControl 
    x:Class="CompanyNamespace.Controls.GoogleMap" 
    xmlns:local="clr-namespace:CompanyNamespace.Controls" 
    ...> 
    <controls:BaseUserControl.Style> 
     <Style> 
      <Setter Property="local:GoogleMap.MapCenter" Value="{Binding Location}" /> 
     </Style> 
    </controls:BaseUserControl.Style> 
    ... 
</controls:BaseUserControl> 

をXML名前空間localはもちろんです同じ場合は冗長ですcontrols


あなたにも同じようUserControlのコンストラクタにバインド作成することができます。あなたが財産を置く場所、それは問題ではないはず基本クラスから継承されているので

public GoogleMap() 
{ 
    InitializeComponent(); 
    SetBinding(MapCenterProperty, new Binding("Location")); 
} 
関連する問題