在WPF中,当缩小离开扩展器时,内容向左移动

时间:2013-04-17 08:46:28

标签: wpf expander

就像Office 2010中的选项卡一样,当我们双击一个选项卡时,菜单将缩小,内容将向上移动。 如下所示: enter image description here

html中的另一个示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>html example</title>
    <style type="text/css">
    .box{width:300px;border:1px solid #ccc;font-size:12px}
    .box dt{line-height:24px;text-align:right;margin:0;padding:0 10px;background:#CCC}
    .box dd{line-height:20px;margin:0}
    #holder2{height:200px}
    </style>
    </head>
    <body>
    <dl class="box">
        <dt id="title">Click to expand/shrink</dt>
        <dd id="holder">
            content
        </dd>
    </dl>
    <br>
    <dl class="box">
        <dt id="title2">Click to expand/shrink</dt>
        <dd id="holder2">
            content
        </dd>
    </dl>
    <script type="text/javascript">

        var Toggle = function(id, minH, maxH, expand, rate, speed){
            this.minH = minH;
            this.maxH = maxH;
            this.tempH = 0;
            this.rate = rate;
            this.speed = speed;
            this.moving = false;
            this.moveT = null;
            this.holder = document.getElementById(id);
            this.expand = expand;
        }
        Toggle.prototype = {
            start : function(){
                if(this.moving) return;
                this.moving = true;
                this.tempH = this.expand ? this.minH : this.maxH;
                var t = this;
                this.moveT = setInterval(function(){t.move()}, this.speed);
            },
            move : function(){
                if(this.expand){
                    if(this.tempH >= this.maxH){
                        this.moving = false;
                        this.expand = false;
                        clearInterval(this.moveT);
                    }else{
                        var h = Math.round((this.maxH - this.tempH) * this.rate);
                        if(h <= 1){
                            h = 1;
                        }
                        this.tempH += h;
                    }
                }else{
                    if(this.tempH <= this.minH){
                        this.moving = false;
                        this.expand = true;
                        clearInterval(this.moveT);
                    }else{
                        var h = Math.round((this.tempH - this.minH) * this.rate);
                        if(h <= 1){
                            h = 1;
                        }
                        this.tempH -= h;
                    }
                }
                this.holder.style.height = this.tempH + "px";
            }
        }
        //module1(default:have shrunk)
        var obj = new Toggle("holder",20,200,true, 0.2, 10);
        document.getElementById("title").onclick = function(){
            obj.start();
        }
        //module1(default:have expanded)
        var obj2 = new Toggle("holder2",20,200,false, 0.2, 10);
        document.getElementById("title2").onclick = function(){
            obj2.start();
        }
    </script>
</body>
</html>

我想缩小Expander左边,然后右边的内容将向左移动。 你能救我吗?

这是我的代码:

<ribbon:RibbonWindow x:Class="WpfRibbonApplication5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    Title="MainWindow"
    x:Name="RibbonWindow"
    Width="640" Height="480">

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Expander Grid.Row="1" Height="300" HorizontalAlignment="Left" Name="expander1" VerticalAlignment="Top" Width="155" ExpandDirection="Right" Margin="12,57,0,0" BorderBrush="Black">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="150*" />
                <RowDefinition Height="148*" />
            </Grid.RowDefinitions>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="36,99,0,0" Name="textBlock1" Text="There're sth." VerticalAlignment="Top" />
        </Grid>
    </Expander>
    <Calendar Grid.Row="1" Height="170" HorizontalAlignment="Left" Margin="173,57,0,0" Name="calendar1" VerticalAlignment="Top" Width="180" />
</Grid>

修改后的代码:

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Expander Grid.Column="0" Height="300" HorizontalAlignment="Left" Name="expander1"
          VerticalAlignment="Top" ExpandDirection="Right" Margin="12,57,0,0" BorderBrush="Black" Expanded="expander1_Expanded">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="150*" />
                <RowDefinition Height="148*" />
            </Grid.RowDefinitions>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="6,99,0,0" 
                   Name="textBlock1" Text="There're sth." VerticalAlignment="Top" Width="128" />
        </Grid>
    </Expander>
    <Calendar Grid.Column="1" HorizontalAlignment="Left" Margin="0,57,0,0" 
          Name="calendar1" VerticalAlignment="Top" />
</Grid>

1 个答案:

答案 0 :(得分:0)

我发现当缩小扩展器时,扩展器也会占用原始区域。 - 那就是因为扩展器不在Height=Auto的行中。以下应该有效。

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Expander Grid.Row="0" Height="300" HorizontalAlignment="Left" Name="expander1"
              VerticalAlignment="Top" Width="155" ExpandDirection="Right" Margin="12,57,0,0" BorderBrush="Black">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="150*" />
                <RowDefinition Height="148*" />
            </Grid.RowDefinitions>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="36,99,0,0" 
                       Name="textBlock1" Text="There're sth." VerticalAlignment="Top" />
        </Grid>
    </Expander>
    <Calendar Grid.Row="1" Height="170" HorizontalAlignment="Left" Margin="173,57,0,0" 
              Name="calendar1" VerticalAlignment="Top" Width="180" />
</Grid>

修改

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Expander Grid.Column="0" Height="300" HorizontalAlignment="Left" Name="expander1"
              VerticalAlignment="Top" Width="155" ExpandDirection="Right" Margin="12,57,0,0" BorderBrush="Black">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="150*" />
                <RowDefinition Height="148*" />
            </Grid.RowDefinitions>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="36,99,0,0" 
                       Name="textBlock1" Text="There're sth." VerticalAlignment="Top" />
        </Grid>
    </Expander>
    <Calendar Grid.Column="1" Height="170" HorizontalAlignment="Left" Margin="173,57,0,0" 
              Name="calendar1" VerticalAlignment="Top" Width="180" />
</Grid>