How does WPF implement a single-choice list control?

Source: Internet
Author: User

WPF does not provide a radiobuttonlist control like ASP. NET. Fortunately, we can use the powerful functions of WPF styles and templates to implement this function with pure XAML code.

<Window X: class = "controltest2.listboxtest2" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: S = "CLR-namespace: system; Assembly = mscorlib"> <window. resources> <X: array type = "{X: type S: string}" X: Key = "data"> <s: String> option1 </s: string> <s: String> option2 </S: String> <s: String> option3 </S: String> </X: array> </window. resources> <Stackpanel datacontext = "{staticresource data}"> <textblock margin = "5"> <textblock text = "current option: "/> <textblock text =" {binding/} "/> </textblock> <ListBox itemssource =" {binding} "issynchronizedwithcurrentitem =" true "width =" 240 "Height = ""60" horizontalalignment = "Left"> <ListBox. itemcontainerstyle> <style targettype = "{X: Type listboxitem}"> <setter property = "template"> <setter. value> <controltem Plate targettype = "{X: Type listboxitem}"> <radiobutton ischecked = "{binding Path = isselected, relativesource = {relativesource templatedparent} "content =" {templatebinding content} "/> </controltemplate> </setter. value> </setter> </style> </ListBox. itemcontainerstyle> </ListBox> <! -- This method is simple --> <stackpanel> <radiobutton groupname = "X"> radio1 </radiobutton> <radiobutton groupname = "X"> Radio2 </radiobutton> <radiobutton groupname = "X"> radio3 </radiobutton> </stackpanel> </WINDOW>

Reference: http://social.msdn.microsoft.com/Forums/zh-CN/wpfzhchs/thread/857fdaa9-5c67-4e0a-a1fd-037f72577c76

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.