This is a migrated thread and some comments may be shown as answers.

[Solved] Sizing controls added to the ContentControl

3 Answers 77 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Gary Paul
Top achievements
Rank 1
Gary Paul asked on 12 Jan 2010, 03:01 PM
I am building a PropertyGrid. It has a list of Property Names and their values. Depending on the requested control type the Value column has different control for each row. I have this working fine.

I implemented this by creating a DataTemplate as mentioned elsewhere in the forum:

 

 

 

<DataTemplate x:Key="RuntimeContentTemplate"> 
    <ContentControl Loaded="ContentControl_Loaded" />

 

</DataTemplate>

Then in the ContentControl_Loaded event I had the appropriate control based on information in the object assigned to each row. The idea here is that we have a class (Property) that defines how we want this value presented in the grid.

 

private void ContentControl_Loaded(object sender, RoutedEventArgs e)

{

 

 

    var contentControl = sender as ContentControl;

 

 

 

    if (contentControl != null)

 

    {

 

        var parentCell = contentControl.ParentOfType<GridViewCell>(); 

 

 

        var column = parentCell.Column as GridViewDataColumn;

 

 

 

 

        var row = parentCell.ParentRow as GridViewRow;

 

 

 

 

        if (row != null)

 

 

        {

 

 

            IProperty p1 = row.Item as IProperty;

 

 

 

            if(p1 != null)

 

            {

 

                Binding binding = new Binding("Value");

                binding.Mode = 

 

BindingMode.TwoWay;

 

 

                switch(p1.ControlType.ToUpper()) 

                {

                    case "TEXTBOX"
                        TextBox textBox = new TextBox();

 

 

                        textBox.SetBinding(TextBox.TextProperty, binding);

 

 

                        contentControl.Content = textBox;

                        textBox.Margin = 

 

new Thickness(0);

 

 

 

                        break;

 

 

                    case "DATEPICKER":

 

 

                        RadDatePicker datePicker = new RadDatePicker();

 

 

                        datePicker.SetBinding(RadDatePicker.SelectedDateProperty, binding);

 

 

                        contentControl.Content = datePicker;

 

                       break;

 

 

 

 

                   case "CHECKBOX":

 

 

  

 

                      CheckBox checkBox = new CheckBox();

 

 

 

                        checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);

 

 

                        checkBox.Margin = 

 

new Thickness(0);

 

 

                        contentControl.Content = checkBox;

 

 

                        break;

 

 

 

 

                    case "COMBOBOX":

 

 

 

 

                        RadComboBox comboBox = new RadComboBox();

 

 

 

                        comboBox.SetBinding(RadComboBox.SelectedItemProperty, binding);

 

 

                        comboBox.Margin = 

 

new Thickness(0);

 

 

 

 

                        switch (p1.Type.ToUpper())

 

 

                        {

 

 

                            case "STRING":

 

 

 

 

                            foreach (string item in ((Property<string>)p1).Constraints)

 

 

                            {

                                comboBox.Items.Add(item);

                            }

 

 

                            break;

 

 

                        }

                        contentControl.Content = comboBox;

 

 

                        break;

 

 

 

                 }

 

            }

       }

    }

I have not found out how to get the controls to fill the cell space. I have set the Margin of each control to 0 with no effect. I have tried setting the controls width to the column width but doesn't make any difference. I have attached a picture showing how the controls do not fill the space of the cell.

3 Answers, 1 is accepted

Sort by
0
Gary Paul
Top achievements
Rank 1
answered on 12 Jan 2010, 04:36 PM
I also tried HorizontalAlignment and VerticalAlignment without any results.
0
Pavel Pavlov
Telerik team
answered on 18 Jan 2010, 09:34 AM
Hello Gary Paul,

Please excuse me for the delayed answer. It seems that the ContentControl in such scenario will always tend to size to  the content and will ignore andy Stretch or margin settings.

I have tried slightly different approach . I have replaced the ContentControl with a Border. It seems the border does not have such problem.

I am setting the Child property  of the border , instead of the Content property of the Content Control.

This seems itsoves  the problem . Something like :
<DataTemplate x:Key="MyCellTemplate" >
                <Border  Loaded="Border_Loaded" />
            </DataTemplate>
private void Border_Loaded(object sender, RoutedEventArgs e)
        {
            var border = sender as Border;
            TextBox textBox = new TextBox();
            textBox.SetBinding(TextBox.TextProperty, new Binding());
  
            border.Child = textBox;
        }


Regards,
Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Gary Paul
Top achievements
Rank 1
answered on 18 Jan 2010, 02:04 PM
Works perfectly! Thanks!
Tags
GridView
Asked by
Gary Paul
Top achievements
Rank 1
Answers by
Gary Paul
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Share this question
or