Denis
Denis

Reputation: 674

How to get the value of the margin of a border?

I'm a beginner in WPF and have to add functionality to someone's user interface. Here's part of the code.

<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>

I'm trying to access the Margin="2" on line 10. When I use this.imageBorder.Margin, I get {0,0,0,0}. How would I get the 2 (of course, this value may change)?

thanks

edit:

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

Upvotes: 0

Views: 984

Answers (1)

Rachel
Rachel

Reputation: 132618

The Border labeled imageBorder does not have its Margin property set to anything, so you are getting the default Margin, which is 0.

To get the Margin property of the Border inside your named border, either give it an x:Name so you can access it in code behind, or look in imageBorder.Child property to find the child Border object, then cast it to a FrameworkElement to get its Margin property.

As for why you're getting {0,0,0,0}, the Margin property is of type Thickness, which consists of properties for Left, Top, Right, and Bottom. If you set a margin to a single value, such as 2, it automatically converts that to a Thickness object with all 4 of its properties set to 2.

Upvotes: 4

Related Questions