7

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

异常详情是这样的

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

WinRT 信息:未能分配给属性“CustomTextBox.Input.Type”。

主页.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;
        }
    }
}

视图模型.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

2 回答 2

10

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

我们都得到了错误!:)

在此处输入图像描述

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

祝你好运。

于 2013-08-02T18:03:07.713 回答
7

不正确

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));
于 2013-08-13T05:57:55.177 回答