Binding to Elementname within resource

Jayme65

Active member
Joined
Apr 5, 2011
Messages
35
Programming Experience
Beginner
Hi,
I can't get to bind to an element name inside a Windows.Resources
I have a grid (with elements inside) in Windows resources.
Among the elements I have a border with an image inside and another border whose background visualbrush visual is binded to the first border (with a render transform and opacity mask to mimic a 'reflection' effect)
This is what it gives when used 'normally'
logoReflection.jpg
VB.NET:
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid Width="266" Height="600">
        <Border Name="Cover" Height="222" Width="242" HorizontalAlignment="Center" VerticalAlignment="Top" Background="#00000000">
            <Image HorizontalAlignment="Center" Stretch="Uniform" VerticalAlignment="Bottom" Source="http://i.telegraph.co.uk/multimedia/archive/02671/Windows_8_logo_2671170c.jpg"/>
        </Border>
        <Border Width="242" Height="222" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,222,0,0" >
            <Border.OpacityMask>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Offset="0" Color="#50000000"/>
                    <GradientStop Offset="0.35" Color="#00000000"/>
                </LinearGradientBrush>
            </Border.OpacityMask>
            <Border.Background>
                <VisualBrush Visual="{Binding ElementName=Cover}">
                    <VisualBrush.RelativeTransform>
                        <ScaleTransform ScaleX="1" ScaleY="-1" CenterX="0.5" CenterY="0.5"/>
                    </VisualBrush.RelativeTransform>
                </VisualBrush>
            </Border.Background>
        </Border>
    </Grid>
</Window>

Now! I HAVE to call this from resources, code behind, so I ended up with this:
VB.NET:
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>       
        <Grid x:Key="LogoGrid" Width="266" Height="600">
            <Border Name="Cover" Height="222" Width="242" HorizontalAlignment="Center" VerticalAlignment="Top" Background="#00000000">
                <Image HorizontalAlignment="Center" Stretch="Uniform" VerticalAlignment="Bottom" Source="http://i.telegraph.co.uk/multimedia/archive/02671/Windows_8_logo_2671170c.jpg"/>
            </Border>
            <Border Width="242" Height="222" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,222,0,0" >
                <Border.OpacityMask>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Offset="0" Color="#50000000"/>
                        <GradientStop Offset="0.35" Color="#00000000"/>
                    </LinearGradientBrush>
                </Border.OpacityMask>
                <Border.Background>
                    <VisualBrush Visual="{Binding ElementName=Cover}">
                        <VisualBrush.RelativeTransform>
                            <ScaleTransform ScaleX="1" ScaleY="-1" CenterX="0.5" CenterY="0.5"/>
                        </VisualBrush.RelativeTransform>
                    </VisualBrush>
                </Border.Background>
            </Border>
        </Grid>        
    </Window.Resources>
    <Grid Name="MainGrid"></Grid>
</Window>
VB.NET:
Class MainWindow 
    Dim LogoGrid As New Grid
    Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        initGameInfoPanel()
    End Sub
    Private Sub initGameInfoPanel()
        LogoGrid = Me.FindResource("LogoGrid")
        MainGrid.Children.Add(LogoGrid)
    End Sub
End Class

...but then I get a 'Cannot find source for binding with reference 'ElementName=Cover'. BindingExpression:(no path)'
What's the problem and how could I resolve it, please?
 
Back
Top