After I posted my previous post on this subject, I done some research on this issue and I found the following:
- The binding between the IsChecked property and a property in the “code behind” works
inonly if the binding is declared inside a Control Template, see code below. - 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).
You can download a sample code (VS2008 Beta2 project) here, Remember to change the .DOC extension to .ZIP.





November 22, 2007 at 2:44 pm |
[...] my first post I described the problem and in second post I proposed and demonstrated a workaround to the [...]
June 2, 2008 at 4:26 pm |
Thank you, I had just stated to work through the possible fixes and here is a solution waiting for me. Ta.
October 17, 2008 at 8:50 am |
Good entry. I have found another solution (in my blog: http://geekswithblogs.net/claraoscura/archive/2008/10/17/125901.aspx)