RadioButton

in namespace DotVVM.BusinessPack.Controls

Renders a RadioButton control similar to HTML radio button.

Usage & Scenarios

Inherits the built-in RadioButton control with a DotVVM Business Pack visual style.

Sample 1: Basic RadioButtons

The RadioButton control has the Checked property of boolean which indicates whether the control is checked or not.

Optionally, you can use the Text property to specify the label for the radio button. Or you can put the label contents inside the bp:RadioButton element.

<bp:RadioButton Checked="{value: IsCompany}" 
                CheckedValue="{value: true}"
                GroupName="RadioGroup" 
                Text="company" />

<bp:RadioButton Checked="{value: IsCompany}" 
                CheckedValue="{value: false}"
                GroupName="RadioGroup">
    person <em>(default)</em>
</bp:RadioButton>

<p>Is company: {{value: IsCompany}}</p>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.RadioButton.sample1
{
    public class ViewModel : DotvvmViewModelBase
    {
        public bool IsCompany { get; set; }
    }
}

Sample 2: Multiple RadioButtons

The RadioButton has also a property CheckedItem which point to a property in the viewmodel. This is an alternative to the Checked property - they cannot be used together.

The property referenced by the CheckedItem will contain the value of the radio button which is checked.

In the example, the second radio button will be checked when the page loads. That's because the initial value of the Color property is "g". If you click another radio button, the property value will be updated immediately.

<bp:RadioButton CheckedItem="{value: Color}" CheckedValue="r" Text="R" />
<bp:RadioButton CheckedItem="{value: Color}" CheckedValue="g" Text="G" />
<bp:RadioButton CheckedItem="{value: Color}" CheckedValue="b" Text="B" />

<p>Selected color: {{value: Color}}</p>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.RadioButton.sample2
{
    public class ViewModel : DotvvmViewModelBase
    {
        public string Color { get; set; } = "g";
    }
}

Sample 3: RadioButton's Changed Event

The RadioButton has the Changed event which is fired whenever the radio button is checked or unchecked.

Notice the CheckedItem and CheckedValue properties. The first radio button will set true to the Value property in the viewmodel, the second one will set false.

The value binding in the CheckedValue must be used, otherwise the true and false values would be treated as strings.

<bp:RadioButton CheckedItem="{value: Value}" CheckedValue="{value: true}" Changed="{command: OnChanged()}" />
<bp:RadioButton CheckedItem="{value: Value}" CheckedValue="{value: false}" Changed="{command: OnChanged()}" />

<p>{{value: NumberOfChanges}}</p>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.RadioButton.sample3
{
    public class ViewModel : DotvvmViewModelBase
    {
        public bool Value { get; set; }

        public int NumberOfChanges { get; set; } = 0;

        public void OnChanged()
        {
            NumberOfChanges++;
        }
    }
}

Sample 4: Real World Sample

Final price is calculated by adding a tax to price of selected beverage. Prices are set in CheckedValue property and the total price is updated after any change by Changed event.

DotVVM Business Pack includes the RadioButtonList, which would make this sample much simpler. If you need to generate multiple radio buttons from a collection in a viewmodel, you can use this control.

<h3>Fruits</h3>
<p>
    <bp:RadioButton CheckedItem="{value: PriceWithoutTax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 1}" 
                    Text="Apple" />

    <bp:RadioButton CheckedItem="{value: PriceWithoutTax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 3}" 
                    Text="Lemon" />

    <bp:RadioButton CheckedItem="{value: PriceWithoutTax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 6}" 
                    Text="Orange" />

    <bp:RadioButton CheckedItem="{value: PriceWithoutTax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 5}" 
                    Text="Pineapple" />
</p>

<h3>Tax</h3>
<p>
    <bp:RadioButton CheckedItem="{value: Tax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 5}" 
                    Text="Reasonable" />

    <bp:RadioButton CheckedItem="{value: Tax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 10}" 
                    Text="Still quite reasonable" />

    <bp:RadioButton CheckedItem="{value: Tax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 20}" 
                    Text="European" />

    <bp:RadioButton CheckedItem="{value: Tax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 40}" 
                    Text="European in 10 years" />
</p>

<p>Total price: ${{value: Price}}</p>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.RadioButton.sample4
{
	public class ViewModel : DotvvmViewModelBase
    {
	    public float PriceWithoutTax { get; set; }

	    public float Tax { get; set; }

	    public float Price { get; set; }

	    public void UpdatePrice()
	    {
	        Price = PriceWithoutTax + ((PriceWithoutTax / 100) * Tax);
	    }

    }
}

Properties

Name Type Description Notes Default Value
property icon AutoFocus Boolean Gets or sets whether the control should have focus when page loads or when a dialog is opened. The default value is false.
attribute
inner element
static value
bindable
default
False
property icon Checked Boolean Gets or sets whether the control is checked.
attribute
inner element
static value
bindable
default
False
property icon CheckedIcon IconBase Gets or sets the icon displayed when the control is checked.
attribute
inner element
static value
bindable
default
null
property icon CheckedItem Object Gets or sets the CheckedValue of the first checked RadioButton in this group.
attribute
inner element
static value
bindable
default
null
property icon CheckedValue Object Gets or sets the value that will be used as a result when the control is checked.
attribute
inner element
static value
bindable
default
null
property icon ContentTemplate ITemplate Gets or sets a template rendering contents of the control.
attribute
inner element
static value
bindable
default
null
property icon Enabled Boolean Gets or sets whether the control is enabled and can be clicked on.
attribute
inner element
static value
bindable
default
True
property icon GroupName String Gets or sets a unique name of the radio button group.
attribute
inner element
static value
bindable
default
null
property icon TabIndex Int32 Gets or sets the order in which the control is reachable in sequential keyboard navigation. The default value is 0 which means the document order.
attribute
inner element
static value
bindable
default
0
property icon Text String Gets or sets the text displayed inside the control.
attribute
inner element
static value
bindable
default
null
property icon Visible Boolean
attribute
inner element
static value
bindable
default
True

Events

Name Type Description
event icon Changed Command Gets or sets the command triggered when the check state is changed.

HTML produced by the control

With no Text or an inner content specified, the control renders just the radio button.

<input type="radio" data-bind="..." />

If there is a Text or an inner content, the label is rendered around the radio button.

<label>
  <input type="radio" data-bind="..." />
  Text or inner content
</label>