Wednesday, April 8, 2009

Binding Radio buttons in XAML

Here's how to to bind a radio button to a list, setting both the button and text portions of the radio button.


    public class ReviewReason : NHibernateEntityBase<ReviewReason>
{
public override int Id
{
get { return ReviewReasonId; }
}

public int ReviewReasonId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool DisplayToUser { get; set; }
public bool Checked {get; set;}
...




Loading the DataContext...

    public partial class ReviewReasonView : IReviewReasonView
{
public ReviewReasonModel Model { get; set; }
public IReviewReasonController Controller { get; set; }

public bool? ShowDialog()
{
this.DataContext = Model;
return base.ShowDialog();
}

...


and the XAML binding...

        <ItemsControl ItemsSource="{Binding Path=ReviewReasons, Mode=TwoWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<RadioButton Name="ReviewReason" GroupName="ReviewReasonGroup"
IsChecked="{Binding Path=Checked}"
Content="{Binding Path=Description}"></RadioButton>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

No comments: