RadioButton Control - The Complete WPF Tutorial

WPF Tutorial TOC Table of Contents

Download as PDF

PDF Download this entire tutorial as PDF, plus all the code samples, right now!

Chapter/article TOC

  1. TextBlock control
  2. TextBlock control - Định dạng nội tuyến
  3. Label
  4. TextBox control
  5. Button control
  6. CheckBox control
  7. RadioButton control
    • RadioButton groups
    • Custom content
  8. PasswordBox control
  9. The Image control

About WPF

  • WPF là gì?
  • WPF vs. WinForms

Bắt đầu

  • Cài đặt Visual Studio Community
  • Hello, WPF!

XAML

  • XAML là gì?
  • XAML cơ bản
  • Events (sự kiện) trong XAML

Một ứng dụng WPF

  • Giới thiệu về 1 WPF Application
  • The Window
  • Làm việc với App.xaml
  • Tham số Command-line trong WPF
  • Resources
  • Bắt lỗi trong WPF
  • Application Culture / UICulture

Các control cơ bản

  • TextBlock control
  • TextBlock control - Định dạng nội tuyến
  • Label
  • TextBox control
  • Button control
  • CheckBox control
  • RadioButton control
  • PasswordBox control
  • The Image control

Control concepts

  • Chú giải control
  • Trang trí Text trong WPF
  • Thứ tự tab
  • Access Keys (Khóa truy cập)

Panels

  • Mở đầu cho chương WPF panels
  • Control Canvas
  • The WrapPanel control
  • StackPanel
  • Dockpanel
  • Control Grid
  • The Grid - Rows & columns
  • The Grid - Units
  • The Grid - Kéo dài một control sang một ô khác (Span)
  • GridSplitter
  • Ứng dụng grid: Tạo 1 biểu mẩu

UserControls & CustomControls

  • Introduction
  • Tạo và sử dụng một UserControl

Data binding

  • Bắt đầu phần WPF data binding
  • Mở đầu về sự ràng buộc quan hệ dữ liệu trong data binding
  • Sử dụng DataContext
  • Data binding thông qua Code-behind
  • Phương thức UpdateSourceTrigger
  • Đáp ứng các thay đổi trong data binding
  • Chuyển đổi giá trị với IValueConverter
  • Thuộc tính StringFormat
  • Debugging data bindings

Các lệnh Commands

  • Giới thiệu về các lệnh WPF
  • Sử dụng WPF commands
  • Thực hiện một WPF Command tùy chỉnh

Hộp thoại

  • The MessageBox
  • Hộp thoại mở file - OpenFileDialog
  • SaveFileDialog
  • The other dialogs
  • Tạo hộp thoại nhập tùy chỉnh

Điều khiển giao diện chung

  • Điều khiển Menu WPF
  • ContextMenu trong WPF
  • Điều khiển WPF ToolBar
  • The WPF StatusBar control
  • Thanh Ribbon

Các điều khiển Rich Text

  • Tổng quan về các điều khiển WPF Rich Text
  • Control FlowDocumentScrollViewer
  • Điều khiển FlowDocumentPageViewer
  • Điều khiển FlowDocumentReader
  • Creating a FlowDocument from Code-behind
  • Advanced FlowDocument content
  • Điều khiển RichTextBox
  • How-to: Tạo một trình biên tập Rich Text

Các điều khiển dạng khác

  • The Border control
  • The Slider control
  • The ProgressBar control
  • The WebBrowser control
  • The WindowsFormsHost control
  • The GroupBox control
  • Điều khiển Lịch
  • Điều khiển DatePicker
  • The Expander control

TabContol

  • Sử dụng WPF TabControl
  • WPF TabControl: Tab positions
  • WPF TabControl: Tạo kiểu cho TabItems

Danh sách control

  • ItemsControl
  • ListBox control
  • Điều khiển ComboBox

ListView

  • Giới thiệu về ListView
  • Một ví dụ ListView đơn giản
  • ListView, data binding and ItemTemplate
  • ListView with a GridView
  • Làm sao để: ListView hiển thị với tên cột được căn trái
  • Phân nhóm trong ListView
  • Sắp xếp trong ListView
  • Làm sao để: sắp xếp cột với ListView
  • ListView filtering

TreeView control

  • Giới thiệu về TreeView
  • Một ví dụ TreeView đơn giản
  • TreeView, data binding and multiple templates
  • TreeView - Selection/Expansion state
  • Lazy loading TreeView items

DataGrid control

  • DataGrid control
  • DataGrid columns
  • DataGrid with row details

Styles

  • Giới thiệu về WPF styles
  • Sử dụng WPF styles
  • Trigger, DataTrigger & EventTrigger
  • WPF MultiTrigger và MultiDataTrigger
  • Trigger animations

Audio & Video

  • Phát âm thanh
  • Phát video
  • Cách thực hiện: Tạo trình phát Audio / Video hoàn chỉnh
  • Tổng hợp giọng nói (làm cho WPF nói)
  • Nhận dạng giọng nói (làm cho WPF lắng nghe)

Misc.

  • DispatcherTimer
  • Đa luồng với BackgroundWorker
  • Hủy bỏ BackgroundWorker

Tạo trò chơi: SnakeWPF

  • Giới thiệu
  • Tạo khu vực trò chơi
  • Tạo và di chuyển con rắn
  • Chuyển động liên tục với DispatcherTimer
  • Thêm thức ăn cho rắn
  • Điều khiển Rắn
  • Phát hiện va chạm
  • Cải thiện SnakeWPF: Làm cho nó trông giống một trò chơi hơn
  • Cải thiện SnakeWPF: Thêm danh sách high score
  • Cải thiện SnakeWPF: Thêm âm thanh
  • Trò chơi hoàn chỉnh & Lời kết
  • WPF
  • ASP.NET MVC
  • ASP.NET WebForms
  • C#
  • CSS3
  • HTML5
  • JavaScript
  • jQuery
  • PHP5

This article has been localized into Vietnamese by the community.

If you are fluent in Vietnamese, then please help us - just point to any untranslated element (highlighted with a yellow left border - remember that images should have their titles translated as well!) inside the article and click the translation button to get started. Or have a look at the current translation status for the Vietnamese language.

If you see a translation that you think looks wrong, then please consult the original article to make sure and then use the vote button to let us know about it.

Metadata

Please help us by translating the following metadata for the article/chapter, if they are not already translated.

If you are not satisfied with the translation of a specific metadata item, you may vote it down - when it reaches a certain negative threshold, it will be removed. Please only submit an altered translation of a metadata item if you have good reasons to do so!

Please login to translate metadata! Already logged in? Please try reloading the page!
  • More info...
  • Looking for the original article in English?
Các control cơ bản: RadioButton control

RadioButton cho phép bạn đưa ra các danh sách các tùy chọn dành cho user, và chỉ được chọn một tùy chọn cùng một thời điểm. Bạn có thể đạt được cùng kết quả và sử dụng ít không gian hơn bằng cách sử dụng Combobox control, nhưng thiết lập radio button đem lại cho người dùng 1 cái nhìn tốt hơn về các tùy chọn họ có.

<Window x:Class="WpfTutorialSamples.Basic_controls.RadioButtonSample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RadioButtonSample" Height="150" Width="250"> <StackPanel Margin="10"> <Label FontWeight="Bold">Are you ready?</Label> <RadioButton>Yes</RadioButton> <RadioButton>No</RadioButton> <RadioButton IsChecked="True">Maybe</RadioButton> </StackPanel> </Window>

Tất cả việc chúng ta cần làm là thêm 1 Label với 1 câu hỏi, và sau đó là 3 cái radio button, mỗi cái là 1 câu trả lời. Chúng ta định nghĩa tùy chọn mặc định bằng cách sử dụng thuộc tính IsChecked trên RadioButton cuối, người dùng có thể thay đổi đơn giản bằng cách nhập chuột trên 1 radio button khác. Đây cũng là thuộc tính bạn có thể muốn sử dụng ở Code-behind để kiểm tra 1 RadioButton được chọn hoặc không.

RadioButton groups (nhóm các control RadioButton)

Nếu bạn thử chạy ví dụ trên, bạn sẽ thấy rằng, như đã nói, chỉ một RadioButton có được chọn tại cùng 1 thời điểm. Nhưng sẽ ra sao nếu bạn muốn một vài nhóm radio button, mỗi nhóm có các lựa chọn của riêng nó? Đây là thời điểm thuộc tính GroupName phát huy vai trò, nó cho phép bạn chỉ định radio button nào được nhóm cùng nhau. Đây là 1 ví dụ:

<Window x:Class="WpfTutorialSamples.Basic_controls.RadioButtonSample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RadioButtonSample" Height="230" Width="250"> <StackPanel Margin="10"> <Label FontWeight="Bold">Are you ready?</Label> <RadioButton GroupName="ready">Yes</RadioButton> <RadioButton GroupName="ready">No</RadioButton> <RadioButton GroupName="ready" IsChecked="True">Maybe</RadioButton> <Label FontWeight="Bold">Male or female?</Label> <RadioButton GroupName="sex">Male</RadioButton> <RadioButton GroupName="sex">Female</RadioButton> <RadioButton GroupName="sex" IsChecked="True">Not sure</RadioButton> </StackPanel> </Window>

Với thuộc tính GroupName được thiết lập trên mỗi radio button, 1 tùy chọn sẽ được chọn trên mỗi group trong 2 group đã cho. Nếu không thiết lập thì chúng ta chỉ có một tùy chọn được chọn trên tất cả 6 radio button.

Tùy chỉnh nội dung

RadioButton thừa kế từ lớp ContentControl, điều này có nghĩa ta có thể tùy chỉnh nội dung và hiển thị. Nếu bạn đánh dấu 1 đoạn text, giống như tôi đã làm ở ví dụ trên, WPF sẽ đặt nó vào bên trong 1 TextBlock control và hiển thị nó, nhưng đây chỉ là 1 lối tắt cho phép làm mọi thứ dễ dàng hơn cho bạn. Bạn có thể sử dụng bất kỳ loại control nào bên trong nó, cũng như bạn sẽ thấy trong ví dụ kế tiếp.

<Window x:Class="WpfTutorialSamples.Basic_controls.RadioButtonCustomContentSample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RadioButtonCustomContentSample" Height="150" Width="250"> <StackPanel Margin="10"> <Label FontWeight="Bold">Are you ready?</Label> <RadioButton> <WrapPanel> <Image Source="/WpfTutorialSamples;component/Images/accept.png" Width="16" Height="16" Margin="0,0,5,0" /> <TextBlock Text="Yes" Foreground="Green" /> </WrapPanel> </RadioButton> <RadioButton Margin="0,5"> <WrapPanel> <Image Source="/WpfTutorialSamples;component/Images/cancel.png" Width="16" Height="16" Margin="0,0,5,0" /> <TextBlock Text="No" Foreground="Red" /> </WrapPanel> </RadioButton> <RadioButton IsChecked="True"> <WrapPanel> <Image Source="/WpfTutorialSamples;component/Images/question.png" Width="16" Height="16" Margin="0,0,5,0" /> <TextBlock Text="Maybe" Foreground="Gray" /> </WrapPanel> </RadioButton> </StackPanel> </Window>

Việc định dạng uyển chuyển, ví dụ này có một chút phức tạp, nhưng ý tưởng thì tương đối đơn giản. Với mỗi RadioButton chúng ta có 1 WrapPanel với 1 hình ảnh và text bên trong nó. Vì bây giờ chúng ta điều khiển text sử dụng TextBlock control, điều này cho phép chúng ta định dạng text theo như chúng ta muốn. Trong ví dụ này, tôi đã thay đổi màu text để phù hợp với lựa chọn. 1 Image control (đọc thêm ở phần sau) được dùng để hiển thị hình ảnh cho mỗi lựa chọn.

Lưu ý bạn có thể nhấp chuột vào bất cứ vị trí nào trên RadioButton, thậm chí trên hình ảnh hoặc text để chọn nó, bởi vì chúng ta đã chỉ định nó như nội dung của RadioButton. Nếu bạn đặt nó như 1 panel riêng biệt cạnh RadioButton thì người dùng chỉ có thể nhấp chuột trực tiếp trên vòng tròn của RadioButton để chọn nó, điều này thì kém thực tế.

The CheckBox control Previous The PasswordBox control Next This article has been fully translated into the following languages:
  • Catalan
  • Chinese
  • Czech
  • Danish
  • Dutch
  • French
  • German
  • Hungarian
  • Italian
  • Japanese
  • Polish
  • Portuguese
  • Russian
  • Slovak
  • Spanish
  • Turkish
  • Ukrainian
  • Vietnamese
Is your preferred language not on the list? Click here to help us translate this article into your language!

Từ khóa » Cách Sử Dụng Radiobutton Trong C#