0

I have created a custom control which will have only one Grid into it.

Here is the part of the code from Generic.xaml

<Style TargetType="{x:Type local:MainView}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MainView }">
                    <Grid x:Name="**PART_MyGrid**" Background="Black" Width="{TemplateBinding Width}"
              Height="{TemplateBinding Height}">
                        <ContentPresenter Content="{TemplateBinding Content}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
 </Style>

Corresponding MainView.cs is as follows:

[TemplatePart(Name = "PART_MyGrid", Type = typeof(Grid))]
    public class MainView : ContentControl
    {
        private Grid MainViewGrid;
        static MainView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MainView), new FrameworkPropertyMetadata(typeof(MainView)));
        }
        public override void OnApplyTemplate()
        {
            //This Function never gets called
            base.OnApplyTemplate();

            //Find the grid in the template once it's applied
            MainViewGrid = base.Template.FindName("**PART_MyGrid**", this) as Grid;
            //We can subscribe to its events
        }
        public void setGrid(DataGrid dtGrid)
        {
***//Exception saying MainViewGrid is null***
            MainViewGrid.Children.Add(dtGrid);
        }
    }

Now I have created a another project and I want to include this custom control into one of the panel programmatically.

This is what I have done in .cs file from different project , where I wanted to create this CustomControl dynamically.

CustomControlLib.MainView m_View = new CustomControlLib.MainView();

***//... Code to create One Datagrid programmatically ...***
   m_View.setGrid(programmatically_created_dataGrid);
   theTabItem.Content = m_View;
   theTabItem.DataContext = m_View.DataContext;

What exactly I want is , I want to create CustomControl dynamically and then add it to TabItem. So, I want to access Grid in CustomControl and add DataGrid to it programmatically. But OnApplyTemplate() gets called only when the Custom control is displayed in the screen. In my case its giving exception saying "MainViewGrid is null" So, how do I access MainView CustomControl's element in this case or rather call OnApplyTemplate(), so that I can "find" Grid and add DataGrid to it.

surajitk
  • 147
  • 4
  • 21
  • Please learn MVVM before you ever write a single line of code in WPF. Also, what you're doing here looks more like a `UserControl`, not a Custom Control. See [MSDN - Control Authoring Overview](http://msdn.microsoft.com/en-us/library/ms745025(v=vs.110).aspx) – Federico Berasategui Jan 02 '14 at 16:06
  • 1
    Iam a bit new to WPF , hence I dont know anything from MVVM perspective. I'll surely try and learn MVVM. But from above code,Iam just not getting the way to create CustomControl programmatically, so that I can access CustomControl's Element in cs file. If you could help, that wud be ideal. – surajitk Jan 02 '14 at 16:36
  • you do NOT need a Custom Control at all. and no you do NOT create or manipulate UI elements in procedural code in WPF. That's what XAML is for. See [How to properly create a dynamically defined TabControl in WPF](http://stackoverflow.com/a/15210593/643085) – Federico Berasategui Jan 02 '14 at 16:39
  • But I want to add a datagrid programmatically into CustomControl/UserControl as a child of CC's Grid. So, I dont know how to do it using XAML. – surajitk Jan 02 '14 at 17:06
  • have you read the link I posted? – Federico Berasategui Jan 02 '14 at 17:07
  • I read the link , but that is more from MVVM perspective. Cant I get the solution without MVVM – surajitk Jan 03 '14 at 11:43
  • no you cannot work in WPF without using proper DataBinding and Data Templating. – Federico Berasategui Jan 03 '14 at 12:05

0 Answers0