我有簡單的WPF佈局任務,並且希望儘可能避免代碼隱藏。XAML中的按鈕處理程序
我有兩個面板左右。當我colapsing左側面板 - 右側面板被拉伸...... 這是我的XAML:
<Grid Name="gridContainer">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Background="Aqua" Grid.Column="0" Name="leftPanel" >
<TextBlock FontSize="35" Foreground="#58290A" TextWrapping="Wrap">Left Hand Side</TextBlock>
</StackPanel>
<GridSplitter Name="leftSplitter" Grid.Column="1" HorizontalAlignment="Stretch"/>
<StackPanel Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Label Content="... Clien Area .. Has to Stretch vertically and horizontally" Margin="10"></Label>
<Button Click="LeftButton_Click" Margin="10">Close Left Panel</Button>
</StackPanel>
</Grid>
這是後臺代碼:
private void LeftButton_Click(object sender, RoutedEventArgs e)
{
if(leftPanel.Visibility == System.Windows.Visibility.Visible)
{
gridContainer.ColumnDefinitions[0].Width = GridLength.Auto;
leftPanel.Visibility = System.Windows.Visibility.Collapsed;
leftSplitter.Visibility = System.Windows.Visibility.Collapsed;
}
else
{
gridContainer.ColumnDefinitions[0].Width = GridLength.Auto;
leftPanel.Visibility = System.Windows.Visibility.Visible;
leftSplitter.Visibility = System.Windows.Visibility.Visible;
}
}
我想知道,有什麼辦法避免這裏的代碼?並僅在XAML中實現此任務?
感謝意見
俗話說,「只因爲你能,不一定意味着你應該」)儘管如此,MVVM真的可以幫助你。 – KodeKreachor 2012-03-28 19:40:31