2

我正在创建一个测试 WPF 项目(计算器)来测试命令绑定和日志记录的实现。基本布局是一个网格。计算器的显示是一个跨越两列并拉伸以填充所有可用空间的文本框。这可以正常工作,但是当主窗体使用 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>
4

1 回答 1

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>

哇,效果更好

于 2012-06-17T23:57:41.937 回答