【问题标题】:wpf can't find control coordinates infowpf找不到控制坐标信息
【发布时间】:2016-07-15 14:44:01
【问题描述】:

我有一个组框,其中包含一个堆栈面板,其中包含一个组合框 (A) 和一个文本框。我还有另一个堆栈面板 (B),其中包含一个组合框和一个标签。 我会使用 xaml 代码将组合框 B 放置在与组合框 A(相同 y)相同的级别上。 请注意,GroupBox 和 Stack Panel(B) 放置在同一行不同列的网格中。

我正在尝试将组合框 (B) 的 y 坐标绑定到组合框 (A) 的 y 坐标。

在 Visual Studio 属性窗口中哪里可以找到 wpf 控件的坐标信息?

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <GroupBox Name="AGroupBox" Grid.Row="1" Grid.Column="0"  >
        <GroupBox.Header>
            <Label Content="GroupBox" FontWeight="Bold" />
        </GroupBox.Header>
        <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
            <ComboBox x:Name="ComboBoxA" Width="100" Height="25" VerticalAlignment="Center"/>
            <TextBlock x:Name="TextBlockA" Width="150" VerticalAlignment="Center" Margin="10,0,0,0" Text="This a Test" />
        </StackPanel>
    </GroupBox>

    <StackPanel Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left">
        <Label x:Name="LabelB" Content="LabelB"  />
        <ComboBox x:Name="ComboBoxB" Width="150" Height="25"/>
    </StackPanel>
</Grid>

【问题讨论】:

  • 请发布您当前的 xaml 标记。屏幕截图也可以提供帮助。

标签: wpf visual-studio combobox controls


【解决方案1】:

使用 y 坐标的近似等价物是使用 Margin 属性...但在第一次调整大小尝试后会中断。

我认为,可以在调整大小时更新 Margin,但我会从更复杂的布局开始,如下所示:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

   <!--additional Grid used to position comboBoxes on the same height-->
    <Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <!--group box is used as cover for the 1st column-->
        <GroupBox Name="AGroupBox" 
                  Grid.Row="0" Grid.RowSpan="3" Grid.Column="0"  >
            <GroupBox.Header>
                <Label Content="GroupBox" FontWeight="Bold" />
            </GroupBox.Header>
        </GroupBox>

        <!--stack panel with ComboBoxA is centered vertically in the 1st column-->
        <StackPanel Grid.Row="1" Grid.Column="0" 
                    Margin="5,0"
                    Orientation="Horizontal">
            <ComboBox x:Name="ComboBoxA" Width="100" Height="25" VerticalAlignment="Center"/>
            <TextBlock x:Name="TextBlockA" Width="150" VerticalAlignment="Center" Margin="10,0,0,0" Text="This a Test" />
        </StackPanel>

        <!--ComboBoxB is centered vertically in the 2nd column-->
        <ComboBox x:Name="ComboBoxB" 
                  Grid.Row="1" Grid.Column="1" 
                  VerticalAlignment="Top" HorizontalAlignment="Left"
                  Width="150" Height="25"/>

        <!--Label attached on top of ComboBoxB-->
        <Label x:Name="LabelB" Content="LabelB" 
               Grid.Row="0" Grid.Column="1" VerticalAlignment="Bottom"/>
    </Grid>
</Grid>

【讨论】:

  • 这解决了我的问题,而无需深入坐标世界。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 2016-02-11
  • 2016-06-18
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 2015-10-20
相关资源
最近更新 更多