开发者

WPF textbox usercontrol containing custom items, not simple text

开发者 https://www.devze.com 2023-01-26 06:29 出处:网络
I\'d like to have a custo开发者_如何学编程m usercontrol based on textbox where I could type in text (for example names) and the names would be then converted to custom items (that would have an X butt

I'd like to have a custo开发者_如何学编程m usercontrol based on textbox where I could type in text (for example names) and the names would be then converted to custom items (that would have an X button to remove them, etc). This is what I would like:

WPF textbox usercontrol containing custom items, not simple text

How can I achieve this? Can I replace a piece of text with a custom item? Thanks for any ideas.


You can do this by changing the control template of the textbox. Since you are using a Usercontrol (and not creating a custom control), all you really need to do is remove the border of the textbox, add a Border around your usercontrol, and put the buttons + borderless textbox into a DockPanel or StackPanel.


Bind your Emails in ListView and give them an ItemTemplate with both an Email and a button.

Bind each button to a Command which deletes an email, passing in the email to delete as the command parameter.

Bind your TextBox to a property - say NewEmail. Make the setter of NewEmail add another email to the Emails's underlying collection, then notify PropertyChanged for NewEmail. The getter should always return an empty string.

When your TextBox changes (by losing focus is the default), the setter will add an email to the Emails and clear the text box. Emails should be a property which returns ObservableCollection<Email>

This will also let you do things like validate each new email is a real email address.

I'll update with some examples if I have time; hope this helps you in the meantime.

Edit: You can also change your textbox's UpdateSourceTrigger=PropertyChanged if you want to react to new emails being added by, say, a semicolon. That will cause the property to update with each change to the text, rather than by losing focus.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号