0

I am adding a control at run-time in a Grid using following code:

void AddControl(UserControl oControl)
{
    grid.Children.Add(oControl);
    oControl.SetValue(Grid.RowProperty, 1);
    oControl.SetValue(Grid.ColumnProperty, 0);
}

I want to remove the control at same position (row = 1, column = 0). I am not retaining reference to the control added earlier. How to get and remove the control at a position (row = 1 and column = 0) from the Grid ?

Brij
  • 11,731
  • 22
  • 78
  • 116
  • http://stackoverflow.com/a/1511802/293712 – Maheep Apr 12 '13 at 14:27
  • `I am adding a control at run-time in a Grid...` You don't normally do that in WPF. It is not a good practice to create/manipulate UI elements in code. Please explain what you need to do and I can tell you the proper way to implement it in WPF. – Federico Berasategui Apr 12 '13 at 14:29
  • @HighCore , I have two user controls which have some functionality in them. In the main window I have two buttons. On click of first I am setting UserControl1 in the second row and on click of second I am setting UserControl2 in the same position. – Brij Apr 12 '13 at 14:32
  • 1
    @Brij Looks like you need a Tab control. – torrential coding Apr 12 '13 at 14:37
  • @Brij agree with torrentialcoding, you need a `TabControl`. – Federico Berasategui Apr 12 '13 at 14:45
  • @HighCore I am just curious. Why adding controls in code behind is not good? – Dilshod Apr 12 '13 at 15:20
  • here is the asnswer :http://stackoverflow.com/questions/12195629/how-to-get-grid-children-by-its-row-and-column-in-wpf – Raghu Dec 19 '14 at 18:24

1 Answers1

3

I have two user controls which have some functionality in them. In the main window I have two buttons. On click of first I am setting UserControl1 in the second row and on click of second I am setting UserControl2 in the same position

What you need in order to achieve that is a TabControl:

    <TabControl>
        <TabItem Header="Tab 1">
            <Grid Background="Gray">
                <TextBlock Text="Here goes UserControl 1"
                           VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </TabItem>

        <TabItem Header="Tab 2">
            <Grid Background="Gray">
                <TextBlock Text="Here goes UserControl 2"
                           VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </TabItem>
    </TabControl>

Result:

enter image description here

Why adding controls in code behind is not good?

Because it creates a maintainability chaos. UI elements must be defined in XAML. That's what XAML is for. Creating UI elements in code behind is not only more code, it's error prone and it completely defeats the separation of UI and code that XAML enables.

What if I need to Dynamically create the UI?

Then you must use DataTemplates defined in XAML. Optionally using DataTriggers to modify the state of UI elements based on certain properties in the Model / ViewModel

WPF's idea of "dynamic" is really really different from traditional UI frameworks.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • +1 for explanation. I am still thinking there are some cases you need to do those things in code behind. Thank you! – Dilshod Apr 12 '13 at 16:24
  • Just a small example. I have List. It contains DateTime, String, Integer .... I want to put DatePicker for DateTime type, I want Slider for a Integer.... Something like that. Is it possible with XAML? – Dilshod Apr 12 '13 at 16:43
  • @Dilshod Yep, Just use a `ListBox` or other `ItemsControl`-derived element, and proper `DataTemplate`s for each type. If you want, post a new question with that and I can give you an example. – Federico Berasategui Apr 12 '13 at 17:03
  • here is question. http://stackoverflow.com/questions/15977354/specific-control-for-a-specific-datatype. Thanks – Dilshod Apr 12 '13 at 17:24
  • @HighCore I agree with your suggestion, but still if we have to get and remove control at a position from the grid, how to do it ? – Brij Apr 15 '13 at 10:24
  • @Brij I don't know, I'm not used to implement those kind of bad practices. If you present me the full situation I can give you my thoughts on that, I'm not going to provide a hacky / bad practice solution. – Federico Berasategui Apr 15 '13 at 14:52