gpt4 book ai didi

XamlParseException 无法分配给属性。绑定(bind)不适用于附加属性

转载 作者:行者123 更新时间:2023-12-02 13:12:40 26 4
gpt4 key购买 nike

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

异常详情是这样的

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

WinRT information: Failed to assign to property '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); }
}
}

最佳答案

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

我们都会收到错误! :)

enter image description here

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

祝你好运。

关于XamlParseException 无法分配给属性。绑定(bind)不适用于附加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17971195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com