I am working on a silverlight application. In this I need to place usercontrols on a canvas in the Page.xaml page dynamically at run time. Later I need to set the data values for the datagrid on these user controls from the Page.xaml itself. I want to set different data for different user controls.
How can i differentiate these user controls from one another and set differnt data to their datagrid from Page.xaml itself.
There are a couple of ways to do this; it just depends on whether or not you are using pure databinding and MVVM. If you are using MVVM and pure databinding, then there's very little that you need to do because the View/ViewModel integration will take care of the hard work for you. If you aren't, then you can assign a name to each control when you assign it using the x:Name property, e.g. x:Name="myUserControl1" (this is if you add it into the XAML). If you add it using code behind, you just assign the name to the Name property.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Either you are instantiating the objects yourself, and can take them with x:Name and have your own reference, or you are using them in a template in the DataGrid. If you are using them in a template, you can hook into the Loaded event and do something with them. For example, you might do this:
I have a pretty basic question to which I want a solution.
I have a bunch of controls on a WPF form. Some controls are within a Grid. I want these controls to stay centered within the grid when maximizing and resizing the window, but as soon as I maximize my window the controls get thrown out of position. I know this probably has someting to do with the Margin property of the controls. Can someone just please tell me how to set the margin or any other property so that the controls stay where I want them too
If you don't want your controls to change size or position, you have to anchor them to *just* the left and top sides of the parent control.
Be aware that if you change the control's position in the designer (as opposed to changing it in the XAML), the designer will make assumptions based on some weird-ass algorithm and will CHANGE the anchors for you. You then have to go back and change them back to what you actually wanted. I've heard that they fixed this behavior in VS2010, but I'm not sure because they claimed they fixed other stuff in 2010, and it *that other stuff) sure as hell ain't fixed.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
Last Visit: 31-Dec-99 19:00 Last Update: 31-Mar-15 11:53