XamlParseException无法分配给属性.绑定不与附加属性一起使用

Xyr*_*oid 6 xaml binding exception microsoft-metro windows-runtime

我想为Windows应用商店应用创建带附加属性的自定义文本框.我正在遵循这个解决方案.现在它使用硬编码值作为属性值,但我想使用绑定设置值,但它不起作用.我试图搜索很多,但没有帮助我任何解决方案.

异常细节是这样的

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

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" />
Run Code Online (Sandbox Code Playgroud)

MainPage.xaml.cs中

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        DataContext = new ViewModel();
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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); }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jer*_*xon 10

这是一个很常见的错误.问题是,绑定目标不能是XAML中的CLR属性.这只是规则.绑定源可以是CLR属性,就好了.目标必须是依赖属性.

我们都得到错误!:)

在此输入图像描述

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

祝你好运.


Xyr*_*oid 6

不正确

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

正确

public static readonly DependencyProperty TypeProperty =
            DependencyProperty.RegisterAttached("Type", typeof(InputType), typeof(Input), new PropertyMetadata(default(InputType), OnTypeChanged));
Run Code Online (Sandbox Code Playgroud)