XamlParseException未能分配给属性。绑定不使用附加属性

XamlParseException未能分配给属性。绑定不使用附加属性,xaml,exception,binding,windows-runtime,microsoft-metro,Xaml,Exception,Binding,Windows Runtime,Microsoft Metro,我想为Windows应用商店应用创建带有附加属性的自定义文本框。我跟在后面。现在它使用硬编码值作为属性值,但我想使用绑定设置值,但它不起作用。我试着搜索了很多,但没有帮助我找到任何解决方案 异常详细信息如下所示 类型为“Windows.UI.Xaml.Markup.XamlParseException”的异常 在CustomTextBox.exe中发生,但未在用户代码中处理 WinRT信息:未能分配给属性 “CustomTextBox.Input.Type” MainPage.xaml <

我想为Windows应用商店应用创建带有附加属性的自定义文本框。我跟在后面。现在它使用硬编码值作为属性值,但我想使用绑定设置值,但它不起作用。我试着搜索了很多,但没有帮助我找到任何解决方案

异常详细信息如下所示

类型为“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" />
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); }
    }
}
public类ViewModel:BindableBase
{
公共视图模型()
{
TextboxInputTypeList=Enum.GetValues(typeof(InputType)).Cast();
}
私有输入类型_SelectedTextboxInputType=InputType.Currency;
公共输入类型SelectedTextboxInputType
{
获取{return\u SelectedTextboxInputType;}
set{this.SetProperty(参考此._SelectedTextboxInputType,value);}
}
私有IEnumerable\u文本框输入类型列表;
公共IEnumerable TextboxInputTypeList
{
获取{return\u TextboxInputTypeList;}
设置{this.SetProperty(参考此._TextboxInputTypeList,value);}
}
}

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

我们都会犯错误!:)

我在这里描述整个事情:

祝你好运。

不正确

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

上述解决方案对我不起作用,因为
Type
属性已经是依赖项属性。此外,我不能编写
(this.Content作为FrameworkElement)Input
中的code>,因为它是静态类&它没有继承
UserControl
类。杰瑞,我要求你演示一下我在问题中提出的问题。谢谢:)你要的和我有时间做的现在还不一致。我很抱歉。同时,这是您最好的示例:嘿,杰瑞,我尝试了很多,但没有得到解决方案:(最好你发布准确的解决方案,我需要到达截止日期。你看了这里吗?我检查了它,但它与我的问题无关。我通过添加
Input.cs
更新了我的问题。我还跟踪了你最新的附加属性,但我无法成功。对你有好处!终于找到了解决方案!
public static readonly DependencyProperty TypeProperty =
            DependencyProperty.RegisterAttached("Type", typeof(InputType), typeof(Input), new PropertyMetadata(default(InputType), OnTypeChanged));