How do I develop parts of an application in XAML and how do I reuse it?
I am working on a main window in my application and I would like to separately compose parts of my window in the Visual Studio designer.
Main window
- Game table (there are actually more of them, so it would be nice to design the game table, mark it as a resource, and then just with some simple code (something like creating a new object and setting the DataContext)).
- Console
- Etc
Is it possible in VS to do this?
I just need to know what to look for, if possible. I don't need a whole solution.
Thanks for the suggestions!
a source to share
The ItemTemplates or UserControl elements are probably what you are looking for.
You can create ItemTemplates for things in the collection so that they are automatically displayed one way or another, and you can directly bind to the data in the class that your ItemTemplate represents.
I often like to create a new UserControl. You are basically creating a new control using XAML. Then you can instantiate and set the datacontext of each as you stated what you want to do.
You can even use it in other XAML projects. Just don't forget to add the namespace. Sort of:
xmlns:lp="clr-namespace:LocalProject"
Then use it in the same way as you would other controls:
<StackPanel>
<lp:YourUserControl DataContext="bind to an object of the correct type here" />
</StackPanel>
And in the code behind you will be able to access the associated DataContext object:
YourCustomClass cc = this.DataContext as YourCustomClass;
a source to share