Version: 1.1 | 2.0 |

RadioButton

in namespace DotVVM.BusinessPack.Controls

Renders the HTML radio button.

Properties

Name Type Description Notes Default Value
Attributes Dictionary<String,Object>
attribute
inner element
static value
bindable
default
AutoFocus Boolean
attribute
inner element
static value
bindable
default
False
Checked Boolean
attribute
inner element
static value
bindable
default
False
CheckedIcon IconBase
attribute
inner element
static value
bindable
default
CheckedItem Object
attribute
inner element
static value
bindable
default
CheckedValue Object
attribute
inner element
static value
bindable
default
ContentTemplate ITemplate
attribute
inner element
static value
bindable
default
Enabled Boolean
attribute
inner element
static value
bindable
default
False
GroupName String
attribute
inner element
static value
bindable
default
TabIndex Int32
attribute
inner element
static value
bindable
default
0
Text String
attribute
inner element
static value
bindable
default
Visible Boolean
attribute
inner element
static value
bindable
default

Events

Name Type Description
Changed Command

Usage & Scenarios

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

HTML Rendered 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>

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 combined.

The property referenced by the CheckedItem, holds 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. he 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 CheckedValues property and the total price is updated after any change by Changed event.

The 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="Pinapple" />
</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 : DotVVM.BusinessPack.DocSamples.Samples.LayoutViewModel
	{
	    public float PriceWithoutTax { get; set; }

	    public float Tax { get; set; }

	    public float Price { get; set; }

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

    }
}

Suggestions


Help & Contribute

The documentation is maintained on GitHub.
We'll be happy if you edit the page and create a pull request.

Send message