简体   繁体   中英

XAML invoke a grid cs element through xaml usercontrol and binding tags

There is an existing code I have got but this is a dll and works with a third party app. I want to run it standalone to see how it works.So I create a grid on the cs coding side and invoke the grid from xaml. I have tried it in several ways but I have just provided the example below. I could not run it at all. Any help is appreciated.

CLASS

public partial class MainWindow : Window
{
    Grid customGrid = new Grid();
    public MainWindow()
    {
        customGrid.Children.Add(xxx);
        customGrid.Children.Add(yyy);
    }
}

XAML

<Grid Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" 
      VerticalAlignment="Top">
    <UserControl Grid.Row="3" HorizontalAlignment="Center"
                 VerticalAlignment="Top">
        <Binding>
            <Binding.Path>customGrid</Binding.Path>
        </Binding>
    </UserControl>
</Grid>

PS:I need to keep UserControl and Binding tags as they are.

Hi I have come up with this and it works as an example for anyone who needs it..

CLASS:

 Grid customGrid = new Grid();
        public MainWindow(){

            InitializeComponent();          

            var tb = new TextBlock();
            tb.Text = "sdasdadsas1";
            customGrid.Children.Add(tb);

            tb = new TextBlock();
            tb.Text = "sdassssdas2";
            customGrid.Children.Add(tb);

            tb = new TextBlock();
            tb.Text = "sdasdas3";
            customGrid.Children.Add(tb);

            this.DataContext = this;

        }

        public Grid gridTest
        {
            get { return customGrid; }
            set { customGrid = value; }
        }

XAML:

   <UserControl Grid.Row="3" >
        <Binding>
            <Binding.Path>gridTest</Binding.Path>
        </Binding>
    </UserControl>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM