none
How to Grid Margin Dynamically

    Question

  • Hi I am having a Grid whose margin is not given by default

    can I get this Grid Margin Dynamically after intializing all child controls within this

    Please Provide solution for this

    Thanks in Advance


    Efforts may fail but don't fail to take effort -------------- sidd

    Tuesday, January 21, 2014 1:05 PM

All replies

  • Hi,

    Yes you can get

            public MainWindow()
            {
                InitializeComponent();
                this.Loaded += MainWindow_Loaded;
            }
    
            void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                /* To get the margin after the child controls are loaded */
                var margin = LayoutRoot.Margin;
                Console.WriteLine("Left:{0}", margin.Left);
                Console.WriteLine("Right:{0}", margin.Right);
                Console.WriteLine("Top:{0}", margin.Top);
                Console.WriteLine("Bottom:{0}", margin.Bottom);
    
                /* To set the margin after the child controls are loaded */
                LayoutRoot.Margin = new Thickness { Left = 10, Bottom = 10, Top = 10, Right = 10 };
                Console.WriteLine("Left:{0}", LayoutRoot.Margin.Left);
                Console.WriteLine("Right:{0}", LayoutRoot.Margin.Right);
                Console.WriteLine("Top:{0}", LayoutRoot.Margin.Top);
                Console.WriteLine("Bottom:{0}", LayoutRoot.Margin.Bottom);
    
            }

    XAML

     <Grid x:Name="LayoutRoot">
            <StackPanel>
                <TextBlock Text="Hai"/>
                <TextBlock Text="Hai"/>
                <TextBlock Text="Hai"/>
                <TextBlock Text="Hai"/>
                <TextBlock Text="Hai"/>
            </StackPanel>
     </Grid>


    srithar

    Tuesday, January 21, 2014 1:28 PM
  • Thanks for replying

    The thing is I am having a gridControl <Grid x:name="demoGrid"/>

    but I can't able to call this .cs file as this Grid is within the style SetterProperty


    Efforts may fail but don't fail to take effort -------------- sidd

    Tuesday, January 21, 2014 1:37 PM
  • Can you post your XAML?

    srithar

    Tuesday, January 21, 2014 1:41 PM