刷新(或调整大小)视图

时间:2013-11-26 20:02:30

标签: wpf mvvm

我可以在一个容器(mainview)中拥有1到4个视图(子视图)。当其他子视图折叠时(通过按钮单击操作),我需要获得一个子视图来动态调整大小并填充主视图。希望下面的内容足以说明这一点。

子视图通过其构造函数加载到主视图中。

MainView代码背后:

// this code is in a foreach
ColumnDefinition column = new ColumnDefinition();
column.Width = new GridLength(10, GridUnitType.Star);
this.gridMainView.ColumnDefinitions.Add(column);
subView.SetValue(Grid.ColumnProperty, 
    this.gridOnMainView.ColumnDefinitions.IndexOf(column));
this.gridOnMainView.Children.Add(subView);

在主视图上,我有一个按钮,通过主视图的视图模型,成功折叠除了一个子视图之外的所有子视图(活动子视图不会折叠)。虽然非活动子视图会崩溃,但活动子视图不会调整大小。

导致操作的主视图上的按钮:

<Button Grid.Row="1" Grid.Column="2" Content="Meter" Command="{Binding HideSubViewsAction}" Width="50" Margin="0,10,0,0" Cursor="Hand" />

子视图中具有绑定可见性属性的网格:

<Grid Visibility="{Binding IsCollapsed, Converter={StaticResource     
    booleanToStringConverter}, ConverterParameter=SubViewVisibility}">
<Grid.RowDefinitions>
    <RowDefinition Height="20*" />
    <RowDefinition Height="25*" />
    <RowDefinition Height="25*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="100*" />
</Grid.ColumnDefinitions>
<Border Grid.Row="0" Grid.Column="0">
    <Viewbox>
        <StackPanel>
            <Label Content="{Binding OutputVoltage}" />
            <Label Content="v" />
        </StackPanel>
    </Viewbox>
</Border>
<Border Grid.Row="1" Grid.Column="0">
    <Viewbox>
        <StackPanel Margin="0,0,0,10">
            <Label Content="{Binding OutputCurrent}" />
            <Label Content="mA" />
        </StackPanel>
    </Viewbox>
</Border>
...Other markup
</Grid>

转换器只检查布尔值IsCollapsed并将true转换为“Collapsed”,将false转换为“Visible”

折叠非活动子视图时,左侧活动子视图不会重新调整大小。当其他子视图折叠时,如何让单独的活动子视图重新调整大小?我不知道主视图中会有多少视图。可以有1到4个子视图。当然,如果只有1,那么我禁用折叠按钮。如果需要,我可以重构代码以不同方式创建子视图。

1 个答案:

答案 0 :(得分:0)

问题是,当所有子视图都已折叠时,您需要进行ColumnSpan更新。一种简单的方法是在构造函数foreach中为每个子视图添加一个绑定:

subView.SetBinding(Grid.ColumnSpanProperty, new Binding("SubViewColumnSpan"));

现在将属性ColumnSpan SubViewColumnSpan添加到您的视图模型中。使用HideSubViewsAction命令将其设置为10(或者至少是网格中列数的数字)。

以上是一个开始,但当然,当重新扩展另一个子视图时,您必须将SubViewColumnSpan属性设置回1。

(注意:鉴于您的需求在复杂性方面越来越大,这种方法很可能会变得繁琐,考虑设计一个custom Panel来处理子视图布局。)

相关问题