2013-07-31 55 views
4

我想爲Windows應用商店應用創建帶有附加屬性的自定義文本框。我正在關注this solution。現在它使用硬編碼值作爲屬性值,但我想使用綁定來設置值,但它不起作用。我試圖搜索很多,但沒有幫助我解決任何問題。XamlParseException無法分配給屬性。綁定不能使用附加屬性

異常的詳細信息是這樣的

類型的「Windows.UI.Xaml.Markup.XamlParseException」 發生在CustomTextBox.exe一個例外,但在用戶代碼

WinRT的信息沒有處理:未能分配給屬性 'CustomTextBox.Input.Type'。

MainPage.xaml中

<!-- local:Input.Type="Email" works --> 
<!-- local:Input.Type="{Binding SelectedTextboxInputType}" not working --> 

<TextBox x:Name="txt" local:Input.Type="{Binding SelectedTextboxInputType}" Height="30" Width="1000" /> 

<ComboBox x:Name="cmb" ItemsSource="{Binding TextboxInputTypeList}" SelectedItem="{Binding SelectedTextboxInputType}" Height="30" Width="200" 
      Margin="451,211,715,527" /> 

MainPage.xaml.cs中

public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     DataContext = new ViewModel(); 
    } 
} 

Input.cs

//InputType is enum 
public static InputType GetType(DependencyObject obj) 
{ 
    return (InputType)obj.GetValue(TypeProperty); 
} 

public static void SetType(DependencyObject obj, InputType value) 
{ 
    obj.SetValue(TypeProperty, value); 
} 

public static readonly DependencyProperty TypeProperty = 
    DependencyProperty.RegisterAttached("Type", typeof(InputType), typeof(TextBox), new PropertyMetadata(default(InputType), OnTypeChanged)); 

private static void OnTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    if (e.NewValue is InputType) 
    { 
     var textBox = (TextBox)d; 
     var Type = (InputType)e.NewValue; 
     if (Type == InputType.Email || Type == InputType.URL) 
     { 
      textBox.LostFocus += OnLostFocus; 
     } 
     else 
     { 
      textBox.TextChanged += OnTextChanged; 
     } 
    } 
} 

ViewModel.cs

public class ViewModel : BindableBase 
{ 
    public ViewModel() 
    { 
     TextboxInputTypeList = Enum.GetValues(typeof(InputType)).Cast<InputType>(); 
    } 

    private InputType _SelectedTextboxInputType = InputType.Currency; 
    public InputType SelectedTextboxInputType 
    { 
     get { return _SelectedTextboxInputType; } 
     set { this.SetProperty(ref this._SelectedTextboxInputType, value); } 
    } 

    private IEnumerable<InputType> _TextboxInputTypeList; 
    public IEnumerable<InputType> TextboxInputTypeList 
    { 
     get { return _TextboxInputTypeList; } 
     set { this.SetProperty(ref this._TextboxInputTypeList, value); } 
    } 
} 

回答

6

錯誤

public static readonly DependencyProperty TypeProperty = 
    DependencyProperty.RegisterAttached("Type", typeof(InputType), typeof(TextBox), new PropertyMetadata(default(InputType), OnTypeChanged)); 

正確

public static readonly DependencyProperty TypeProperty = 
      DependencyProperty.RegisterAttached("Type", typeof(InputType), typeof(Input), new PropertyMetadata(default(InputType), OnTypeChanged)); 
+1

對你有好處!終於解決! –

9

這是一個很常見的錯誤。問題是,綁定目標不能是XAML中的CLR屬性。這只是規則。綁定源可以是CLR屬性,就好了。目標只是依賴屬性。

我們都有錯誤! :)

enter image description here

我在這裏描述了整個事情:http://blogs.msdn.com/b/jerrynixon/archive/2013/07/02/walkthrough-two-way-binding-inside-a-xaml-user-control.aspx

好運。

+0

上述解決方案,因爲'Type'屬性已經爲依賴屬性並沒有爲我工作。此外,我不能在'Input'類的構造函數中編寫'(this.Content as FrameworkElement).DataContext = this;'因爲它是靜態類,它不會繼承'UserControl'類。傑裏,我請你演示一下我在問題中提出的問題。謝謝:) – Xyroid

+0

你問什麼,我有什麼時間沒有對齊。對不起。與此同時,這是你最好的示例:http://winrtxamltoolkit.codeplex.com/SourceControl/latest#WinRTXamlToolkit/Controls/NumericUpDown/NumericUpDown.cs –

+0

嘿傑裏,我嘗試了很多,但沒有得到解決方案:(更好你發佈確切的解決方案,我需要達到最後期限 – Xyroid