如何使用ColumnSpan使ViewBox在Grid Cell内拉伸?

时间:2012-06-17 19:12:04

标签: wpf layout

我正在创建一个测试WPF项目(计算器)来测试命令绑定和日志记录的实现。基础布局是网格。计算器的显示是一个跨越两列并延伸以填充所有可用空间的TextBox。这工作正常,但我想在使用ViewBox调整主窗体大小时调整显示中的文本大小。我尝试以多种方式介绍ViewBox,但是当显示中的文本调整大小时,TextBox不再延伸以填充所有空间。

我该如何解决这个问题? XAML如下。

<Grid>
        <Grid.Resources>
            <Style TargetType="Label">
                <Setter Property="Margin" Value="5"/>
            </Style>
            <Style TargetType="TextBox">
                <Setter Property="Margin" Value="5"/>
            </Style>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="5"/>
            </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Grid.Column="0" Grid.Row="1">M+</Button>
        <Button Grid.Column="0" Grid.Row="2">MR</Button>
        <Button Grid.Column="1" Grid.Row="2"
                Click="Button_Click">1</Button>
        <Button Grid.Column="2" Grid.Row="2"
                Click="Button_Click">2</Button>
        <Button Grid.Column="3" Grid.Row="2"
                Click="Button_Click">3</Button>
        <Button Grid.Column="1" Grid.Row="3"
                Click="Button_Click">4</Button>
        <Button Grid.Column="2" Grid.Row="3"
                Click="Button_Click">5</Button>
        <Button Grid.Column="3" Grid.Row="3"
                Click="Button_Click">6</Button>
        <Button Grid.Column="1" Grid.Row="4"
                Click="Button_Click">7</Button>
        <Button Grid.Column="2" Grid.Row="4"
                Click="Button_Click">8</Button>
        <Button Grid.Column="3" Grid.Row="4"
                Click="Button_Click">9</Button>
        <Button Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="5"
                Click="Button_Click">0</Button>
        <Button Grid.Column="3" Grid.Row="5"
                Click="Button_Click">.</Button>
        <Button Grid.Column="4" Grid.Row="2">=</Button>
        <Button Grid.Column="4" Grid.Row="3">+</Button>
        <Button Grid.Column="4" Grid.Row="4">-</Button>
        <Button Grid.Column="5" Grid.Row="3">x</Button>
        <Button Grid.Column="5" Grid.Row="4">:</Button>
        <Button Grid.Column="4" Grid.Row="1">C</Button>
        <Button Grid.Column="5" Grid.Row="1">CE</Button>
        <Viewbox Grid.Column="1" Grid.Row="1">
            <Label >M</Label>
        </Viewbox>
        <!-- Display -->
        <TextBox Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="2"  
                 HorizontalContentAlignment="Right"
                 Text="{Binding CurrentValue}"/>
    </Grid>

1 个答案:

答案 0 :(得分:1)

<Viewbox Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="2" >
        <TextBox Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Viewbox}, Path=ActualWidth}"    
             HorizontalContentAlignment="Stretch" 
             Text="{Binding CurrentValue}"/>
</Viewbox>

这对我有用。然后我的伙伴去了!就这样做

<Viewbox Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="2" >
        <TextBox Width="100"     
         HorizontalContentAlignment="Stretch"  
         Text="{Binding CurrentValue}"/>
</Viewbox>

并且哇工作得更好