在后台代码中设定控件的Visibility
在不同情况下,显示不同的控件。
Case1:ToggleButton显示,Grid的Visibility通过ToggleButton的IsChecked属性绑定;
Case2:ToggleButton隐藏,Grid显示。
通过类型判断进入处理部分:
1 public HelloWindow(WinType type) 2 { 3 winType = type; 4 5 InitializeComponent(); 6 7 if (winType == WinType.Case1) 8 { 9 myToggleButton.Visibility = System.Windows.Visibility.Collapsed; 10 myGrid.Visibility = System.Windows.Visibility.Visible; 11 } 12 else if (winType == WinType.Case2) 13 { 14 myToggleButton.Visibility = System.Windows.Visibility.Visible; 15 Binding binding = new Binding() 16 { 17 ElementName = "myToggleButton", 18 Path = new PropertyPath("IsChecked"), 19 Converter = (BooleanToVisibilityConverter)this.FindResource("b2v"), 20 Mode = BindingMode.OneWay 21 }; 22 myGrid.SetBinding(Grid.VisibilityProperty, binding); 23 // 或者使用静态方法 24 // BindingOperations.SetBinding(myGrid, Grid.WidthProperty, binding); 25 } 26 }
—EOF—
/**************************************************************************
                 
原文来自博客园——Submarinex的博客: www.cnblogs.com/submarinex/               
 
*************************************************************************/