Home:ALL Converter>SL 4: bind tabitem visibility to checkbox

SL 4: bind tabitem visibility to checkbox

Ask Time:2011-05-17T05:24:05         Author:Number8

Json Formatter


When I create a new TabItem I create a new CheckBox at the same time.
I would like to bind the TabItem's visibility to the CheckBox, but am having trouble setting up the binding.

                CheckBox cb = new CheckBox();
                cb.Content = tabItemName;
                cb.Name = tabItemName;

                var visibilityBinding = new Binding("IsChecked");
                visibilityBinding.Mode = BindingMode.TwoWay;
                visibilityBinding.ElementName = cb.Name;  // this could just be a string literal 
                visibilityBinding.Converter = new VisibilityConverter();
                m.TabItem.SetBinding(TabItem.VisibilityProperty, visibilityBinding);

                cb.IsChecked = true;

At runtime I get this error:
System.Windows.Data Error: BindingExpression path error: 'IsChecked' property not found on 'Telerik.Windows.Controls.RadTabItem' 'Telerik.Windows.Controls.RadTabItem' (HashCode=66442352). BindingExpression: Path='IsChecked' DataItem='Telerik.Windows.Controls.RadTabItem' (HashCode=66442352); target element is 'Telerik.Windows.Controls.RadTabItem' (Name='Central ...'); target property is 'Visibility' (type 'System.Windows.Visibility')..

What am I doing wrong? I thought the argument to the Binding ctor was the property on the source object (the CheckBox) that being bound to the property named in the SetBinding() call..

Thanks for any advice...

UPDATE:
I added
visibilityBinding.Source = cb;

and get this error at runtime:
Binding.RelativeSource cannot be set while using Binding.Source

Author:Number8,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6023623/sl-4-bind-tabitem-visibility-to-checkbox
yy