2015-10-17 26 views
0

任何人都可以解釋如何評估XAML數據綁定表達式嗎?我有一個註冊屬性VisualState的控件。使用XAML數據綁定時發生UWP InvalidCastException

public CardStates VisualState 
{ 
    get 
    { 
     return (CardStates)this.GetValue(VisualStateProperty); 

    } 
    set 
    { 
     this.SetValue(VisualStateProperty, value); 
    } 
} 

public static readonly DependencyProperty VisualStateProperty = DependencyProperty.RegisterAttached("VisualStateProperty", typeof(CardStates), typeof(StateManager), new PropertyMetadata(null, (s, e) => { })); 

在xaml中,我嘗試將值綁定到此屬性。 State存在父級的DataContext對象。

<local:CardControl VisualState="{Binding State.Value}" /> 

在XamlTypeInfo.g.cs生成的代碼看起來是這樣的

private void set_4_CardControl_VisualState(object instance, object Value) 
{ 
    var that = (global::MeetEric.UI.Controls.CardControl)instance; 
    that.VisualState = (global::MeetEric.ViewModels.CardStates)Value; 
} 

此代碼引發InvalidCastException因爲值的值是一個Windows.UI.Xaml.Data.Binding對象。

我是否錯過了一些顯而易見的事情來啓用數據綁定?我需要某種形式的轉換器嗎?

+0

看起來像幾個錯誤;你想創建一個附屬的財產或一個普通的財產? –

回答

0

嘗試將typeof(StateManager)更改爲typeof(CardControl)。 (在DependencyProperty.RegisterAttached)

此參數要求DependencyProperty的所有者。