XamlParseException Failed to assign property. Binding does not work with attached property

I want to create a custom text box with an attached property for a Windows Store application. I follow this decision . Now it uses a hard-coded value as the value of the property, but I want to set the value using binding, but it does not work. I tried to search a lot, but did not help me in the decision.

Exclusion details look like this

An exception like "Windows.UI.Xaml.Markup.XamlParseException" occurred in CustomTextBox.exe, but was not processed in the user code

WinRT Info: Failed to set property '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); } } } 
+4
source share
2 answers

Incorrect

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

Right

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

This is a fairly common mistake. The problem is that target binding cannot be CLR properties in XAML. These are just the rules. The binding source can be the CLR property, just fine. Goals just have to be dependent properties.

We all get the error! :)

enter image description here

Here I describe everything: http://blogs.msdn.com/b/jerrynixon/archive/2013/07/02/walkthrough-two-way-binding-inside-a-xaml-user-control.aspx

Good luck.

+9
source

Source: https://habr.com/ru/post/1494498/


All Articles