【问题标题】:XamlParseException Failed to assign to property. Binding not working with attached propertyXamlParseException 未能分配给属性。绑定不适用于附加属性
【发布时间】:2013-08-01 00:38:22
【问题描述】:

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

异常详情是这样的

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

输入.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); }
    }
}

【问题讨论】:

    标签: xaml exception binding windows-runtime microsoft-metro


    【解决方案1】:

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

    我们都得到了错误! :)

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

    祝你好运。

    【讨论】:

    • 上述解决方案对我不起作用,因为Type 属性已经是依赖属性。此外,我不能在类Input 的构造函数中写(this.Content as FrameworkElement).DataContext = this;,因为它是静态类并且它没有继承UserControl 类。杰瑞,我要求你演示一下我在问题中提出的问题。谢谢:)
    • 你的要求和我有时间做的事情现在不一致。对不起。同时,这是您最好的示例:winrtxamltoolkit.codeplex.com/SourceControl/…
    • 嘿,杰瑞,我尝试了很多,但没有得到解决方案:(最好你发布确切的解决方案,我需要达到截止日期。
    • 我检查了它,但它与我的问题无关。我通过添加Input.cs 更新了我的问题。我还关注了您最新的blog post 附加财产,但我无法成功。
    【解决方案2】:

    不正确

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

    【讨论】:

    • 对你有好处!终于解决了!
    猜你喜欢
    • 1970-01-01
    • 2020-01-29
    • 2015-05-07
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多