none
How get the brush's parent. in Win8 RRS feed

  • 问题

  • Code:

    <Grid HorizontalAlignment="Left" Height="526" VerticalAlignment="Top" Width="711" Margin="237,117,0,0">
            <Grid.Background>
                <ImageBrush ImageSource="Assets/A12122_008.JPG" Stretch="None"ImageOpened="ImageBrush_ImageOpened"/>
    </Grid.Background>
    </Grid>

    I want to know how to get the grid. I want to animation the Grid.


    2013年8月23日 10:13

答案

  • Hello Guys,

    From your description, I know there is an issue regarding how to get brush’s parent.

    In WPF, we can use VisualTreeHelper and LogicTreeHelper to implement this function. I create a sample on my side. Something looks like this,

    XAML Code:

    <Window x:Class="VisualTreeGetParentApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Get Element Parent" Height="200" Width="200">
        <Grid>
            <StackPanel x:Name="sp">
                <TextBox Text="I am a texbox" Margin="5"/>
                <Button x:Name="GetParent" Click="GetParent_Click" Content="Get Parent" Margin="5"/>
            </StackPanel>
        </Grid>
    </Window>

    Code behind.

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void GetParent_Click(object sender, RoutedEventArgs e)
            {
                Visual grid = (Grid)VisualTreeHelper.GetParent(sp);
                MessageBox.Show(grid.ToString());
            }
        }

    The result is shown in the following figure.

    Here I recommended some articles to you, it will be help you to understand VisualTreeHelper & LogicTreeHelper technology better.

    #Understanding the Visual Tree and Logical Tree in WPF

    http://www.codeproject.com/Articles/21495/Understanding-the-Visual-Tree-and-Logical-Tree-in

    #Logical- and Visual Tree

    http://wpftutorial.net/LogicalAndVisualTree.html

    If you need further assistance, please do not hesitate to tell me.

    Regards,


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年8月26日 7:12
    版主