RadioButton IsChecked Binding Problem – Conclusions

After I posted my previous post on this subject, I done some research on this issue and I found the following:

  1. The binding between the IsChecked property and a property in the “code behind” works in only if the binding is declared inside a Control Template, see code below.
  2. When binding between xaml elements the problem does not occur.

<UserControl

    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

    x:Class=RadioButtonTest.TemplatedControl

    Height=300 Width=300 Template={DynamicResource UserControlControlTemplate1}>

  <UserControl.Resources>

    <BooleanToVisibilityConverter x:Key=BooleanToVisibilityConverter/>

    <ControlTemplate x:Key=UserControlControlTemplate1 TargetType={x:Type UserControl}>

      <Grid>

        <StackPanel VerticalAlignment=Top>

          <StackPanel VerticalAlignment=Top>

 

            <RadioButton

                Height=16

                HorizontalAlignment=Left

                Name=isEllipse

 

                IsChecked={Binding Path=IsEllipse,

                                        RelativeSource={RelativeSource TemplatedParent},

                                        Mode=TwoWay,

                                        UpdateSourceTrigger=PropertyChanged}

                Content=Ellipse/>

 

            <RadioButton

                Height=16

                HorizontalAlignment=Left

                Name=isRectangle

                Content=Rectangle/>

 

          </StackPanel>

          <TextBlock Text=Binding Visibilty to Radio Button IsChecked />

          <TextBlock Text=property with BooleanToVisibilityConverter:/>

          <Ellipse x:Name=EllipseTip Fill=#FF000000

                  Width=30 Height=30  StrokeThickness=1

                  Visibility={Binding Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=isEllipse, Mode=Default}/>

          <Rectangle x:Name=RectangleTip Fill=#FF000000

                    Width=30 Height=30 StrokeThickness=1

                      Visibility={Binding Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=isRectangle, Mode=Default}/>

          <TextBlock Text=Binding to SelectionText Property />

          <TextBlock Text=and IsEnabled to rectangle radio button:/>

          <TextBox Text={Binding Path=SelectionText, RelativeSource={RelativeSource TemplatedParent}, Mode=Default}

          IsEnabled={Binding Path=IsChecked, ElementName=isRectangle, Mode=Default}/>

        </StackPanel>

      </Grid>

    </ControlTemplate>

  </UserControl.Resources>

</UserControl>

I did a sample which uses two similar controls with the difference that one uses a control template and the other do not, and demonstrates the differences while binding to properties on the UserControls (TemplatedControl and NoTemplateControl).

radioBtnTest

You can download a sample code (VS2008 Beta2 project) here, Remember to change the .DOC extension to .ZIP.

3 Responses to “RadioButton IsChecked Binding Problem – Conclusions”

  1. RadioButton IsChecked Binding Problem in .NET 3.5 RTM « WPF, .NET & Other stuff Says:

    [...] my first post I described the problem and in second post I proposed and demonstrated a workaround to the [...]

  2. Oli Says:

    Thank you, I had just stated to work through the possible fixes and here is a solution waiting for me. Ta.

  3. ClaraOscura Says:

    Good entry. I have found another solution (in my blog: http://geekswithblogs.net/claraoscura/archive/2008/10/17/125901.aspx)

Leave a Reply