SL 4: bind tabitem visibility to checkbox
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.SourceBinding.Source() is necessary for binding programmatically, and is exclusive with Binding.ElementName.
So, this works:
var visibilityBinding = new Binding("IsChecked");
visibilityBinding.Source = cb;
visibilityBinding.Mode = BindingMode.TwoWay;
// visibilityBinding.ElementName = cb.Name;
visibilityBinding.Converter = new VisibilityConverter();
m.TabItem.SetBinding(RadTabItem.VisibilityProperty, visibilityBinding);
Hope this helps someone else....
精彩评论