如何获得边界边际的价值?

时间:2012-10-04 16:32:09

标签: wpf wpf-controls wpfdatagrid

我是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)));

1 个答案:

答案 0 :(得分:4)

标记为imageBorder的边框没有将Margin属性设置为任何内容,因此您将获得默认的Margin,即0。

要在命名边框内获取Border的Margin属性,请为其指定x:Name,以便可以在后面的代码中访问它,或者查看imageBorder.Child属性以查找子Border对象,然后将其投射到FrameworkElement以获得其保证金属性。

至于您获得{0,0,0,0}的原因,Margin属性的类型为Thickness,其中包含Left,Top,Right和Bottom的属性。如果将边距设置为单个值(例如2),它会自动将其转换为Thickness对象,并将其所有4个属性设置为2。