开发者

SL 4: bind tabitem visibility to checkbox

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


Binding.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....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜