简体   繁体   English

仅单向绑定到TextBox

[英]Binding to TextBox only one-way

I need to bind a List<Key> to TextBox . 我需要将List<Key>绑定到TextBox I've written a converter which converts from List<Key> to string (the ConvertBack method just throws an exception - it isn't used). 我编写了一个将List<Key>转换为字符串的转换器( ConvertBack方法只会引发异常-未使用)。 Problem is that when I try to use this converter, it shows me an error: The TypeConverter for "IValueConverter" does not support converting from a string. 问题是,当我尝试使用此转换器时,它显示了一个错误: “ IValueConverter”的TypeConverter不支持从字符串转换。 It appears that the problem is caused because it's trying to convert string from TextBox to List<Key>, but I don't want to do that (user won't "write" to this TextBox ). 看来是由于尝试将stringTextBox转换为List<Key>,但我不想这样做(用户不会“写入”此TextBox )。 I've already thought about using something else than TextBox - perhaps TextBlock which I think would solve the problem, but I'd like to use TextBox for some other reasons. 我已经考虑过使用TextBox以外的其他东西-也许是TextBlock ,我认为它可以解决问题,但是出于某些其他原因,我想使用TextBox

My property I want to bind: 我要绑定的属性:

public partial class Settings : Window
{
    public List<Key> hotkeyCapture_keys { get; set; }
    ...

Converter: 转换器:

class ListKeyToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Join(" + ", (List<Key>)value);
    }
    ...

And TextBox: 和文本框:

<TextBox ... Text="{Binding hotkeyCapture_keys, Converter=ListKeyToStringConverter}" />

When try to Build it, the app immediately shuts down with "XamlParseException occured" . 尝试对其进行构建时,该应用程序会立即关闭并显示“ XamlParseException created”

Can anyone tell me how to solve this problem? 谁能告诉我如何解决这个问题?

Thanks 谢谢

Not sure if this is really something you want to do. 不知道这是否真的是您想要做的。 If you want a read-only TextBox there are other ways of doing so out there already; 如果您想要一个只读的TextBox那么已经有其他方法可以使用了。 one that you mentioned, another being RichTextBox . 您提到的一个是RichTextBox But I'll assume you know what you want. 但是我假设您知道您想要什么。

You'll first want to use this in your Convert (don't forget to add using System.Linq; ): 您首先要在Convert使用它(不要忘记using System.Linq;进行添加):

return string.Join(" + ", ((List<Key>)value).Select(x=> x.Name)); 
//where Name is a public property in type Key

Secondly--bingo, TextBox.Text binds Mode=TwoWay by default. 其次-宾果,默认情况下TextBox.Text绑定Mode=TwoWay You can change that to OneWay or OneTime : 您可以将其更改为OneWayOneTime

<TextBox ... Text="{Binding hotkeyCapture_keys, Mode=OneTime, Converter=ListKeyToStringConverter}" />

Also, you're throwing an exception. 另外,您将引发异常。 Something has to handle that exception. 必须处理一些异常。 Nothing is. 没有什么是。 Instead of throwing an exception in ConvertBack , just call return null; 无需在ConvertBack引发异常,只需调用return null;

change your return value in converter to 将您在转换器中的返回值更改为

 return String.Join(" + ", ((List<Key>)value).ToArray());

you can have a look at WPF textblock binding with List<string> 您可以看看使用List <string>的WPF文本块绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM