XamlParseException无法分配给属性。绑定不能与附带的属性一起使用 [英] XamlParseException Failed to assign to property. Binding not working with attached property

查看:202
本文介绍了XamlParseException无法分配给属性。绑定不能与附带的属性一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Windows Store应用程序创建具有附加属性的自定义文本框。我正在关注此解决方案。现在它使用硬编码值作为属性值,但是我想使用绑定设置值,但它不工作。我试图搜索很多但没有帮助我任何解决方案。

I want to create custom text box with attached property for Windows Store app. I am following this solution. Now it uses hard coded value as property value but I want to set value using binding, but it's not working. I tried to search a lot but didn't helped me any solution.

异常细节就是这样


CustomTextBox.exe中出现类型为Windows.UI.Xaml.Markup.XamlParseException
的异常,但未在用户代码中处理

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in CustomTextBox.exe but was not handled in user code

WinRT信息:无法分配到属性
'CustomTextBox.Input.Type'。

WinRT information: Failed to assign to property 'CustomTextBox.Input.Type'.

MainPage.xaml

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

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

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); }
    }
}


推荐答案

不正确

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));

这篇关于XamlParseException无法分配给属性。绑定不能与附带的属性一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆