0

我是 WPF 的初学者,必须向某人的用户界面添加功能。这是代码的一部分。

<Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" >
        <Grid Grid.Row="0">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition Width="{Binding ElementName=passFailIndicator, Path=Width}"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Border Background="Black" local:StretchPanel.Proportion="1" Name="imageBorder" Grid.Column="0">
                <Border BorderThickness="1" Margin="2" BorderBrush="Green" HorizontalAlignment="Left" VerticalAlignment="Top" Width="{Binding ElementName=imageBorder, Path=Width}">
                    <Grid Width="{Binding ElementName=imageBorder, Path=Width}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="{Binding ElementName=imageSelectExpander, Path=Width}"/>
                        </Grid.ColumnDefinitions>
                        <Image Grid.Column="0" x:Name="image" Source="{Binding DisplayImage, Mode=OneWay}" VerticalAlignment="Top" Stretch="Uniform" HorizontalAlignment="Left" StretchDirection="Both" MouseMove="image_MouseMove" />
                        <TextBlock Name="pxPos" Text="mouse position" HorizontalAlignment="Right" VerticalAlignment="Bottom" MaxHeight="20" Foreground="Aqua"></TextBlock>
                        <Expander Grid.Column="1" VerticalAlignment="Top" Name="imageSelectExpander">
                            <ComboBox x:Name="imageSelect" ItemsSource="{Binding AvailableImages, Mode=OneWay}" SelectedIndex="{Binding ImageSelect, Mode=TwoWay}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="4" Padding="4" MaxHeight="40"></ComboBox>
                        </Expander>
                    </Grid>
                </Border>
            </Border>
            <Border x:Name="passFailIndicator" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100" Height="100">
                <Viewbox Stretch="Uniform" >
                    <Path Style="{Binding TestResult, Mode=OneWay, Converter={StaticResource testResultToPathStyle}}" Margin="2" />
                </Viewbox>
            </Border>
        </Grid>
    </Border>

我正在尝试访问第 10 行的 Margin="2"。当我使用 this.imageBorder.Margin 时,我得到 {0,0,0,0}。我将如何获得 2(当然,这个值可能会改变)?

谢谢

编辑:

FrameworkElement fe = (FrameworkElement)this.imageBorder.Child;
            pxPos.Text = (string.Format("x:{0} y:{1}", (int)((double)(pt.X - fe.Margin.Left) * (double)this.image.Source.Width / this.image.ActualWidth), (int)((double)(pt.Y - fe.Margin.Top)*(double)this.image.Source.Height / this.image.ActualHeight)));
4

1 回答 1

4

标记的 BorderimageBorder没有将其Margin属性设置为任何值,因此您将获得默认值Margin,即 0。

要在命名边框内获取 Border 的 Margin 属性,请给它一个x:Name以便您可以在后面的代码中访问它,或者查看imageBorder.Child属性以找到子 Border 对象,然后将其强制转换为 aFrameworkElement以获取其 Margin 属性。

至于你为什么得到{0,0,0,0}Margin属性的类型是Thickness,它由 Left、Top、Right 和 Bottom 的属性组成。如果您将边距设置为单个值,例如 2,它会自动将其转换为Thickness所有 4 个属性都设置为 2 的对象。

于 2012-10-04T16:50:41.327 回答