Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,I am quite new in WPF, I would like to define dynamically the grid of my aplication. I am trying to bind the number of rows and columns dynamically with next code, but what I am obtaining are parse errors . I looking for it in google and I am only finding info about how to bind values through data templates.



XML
<Grid  x:Name="LayoutRoot" Style="{StaticResource ContentRoot}">
     <Grid  ShowGridLines="True"  x:Name="rootGrid" >
       <Style>
         <Setter Property="Grid.Row" Value="{Binding GridRow}" />
         <Setter Property="Grid.Column" Value="{Binding GridColumn}" />
       </Style>

       <Viewbox Grid.Column="0" Grid.Row="0">
         <UniformGrid x:Name="thruster_panel_port"  Margin="10">
         </UniformGrid>
       </Viewbox>

       <Viewbox Grid.Column="0" Grid.Row="1">
         <UniformGrid x:Name="thruster_panel_azimuth" Margin="10">
         </UniformGrid>
       </Viewbox>

       <Viewbox Grid.Column="1">
         <UniformGrid  x:Name="thruster_panel_bridge"  Grid.Column="1" >
         </UniformGrid>

       </Viewbox>
       <Viewbox Grid.Column="2">
         <UniformGrid x:Name="thruster_panel_stdboard" Grid.Column="2" >
         </UniformGrid>




In the cs side:

C#
public class dynamic_Layout
    {
        int _gridRow;
        int _gridColumn;

        public int GridRow
        {

            get
            {
                return _gridRow;
            }
            set
            {
                _gridRow = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("GridRow"));
                }
            }
        }

        public int GridColumn
        {

            get
            {
                return _gridColumn;
            }
            set
            {
                _gridColumn = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("GridColumn"));
                }
            }
        }


        public dynamic_Layout(int columns,int rows) {

            this.GridColumn = columns;
            this.GridRow = rows;
        
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


    }


 public DynamicUI()
        {
             
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);   
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {

           dynamic_Layout _layout = new dynamic_Layout(3,4);
            this.rootGrid.DataContext =_layout;
            CreateUI();//This metod add usercontrols to the grid , in the UniformGrids.
        }


Could someone point me in the right direction?SHould I do something with the ColumnsDefinitons in XALM?
Posted
Updated 15-Sep-14 22:18pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900