-1

I have the Main view:

<Window ...>
        <WrapPanel Name="Container" Height="500" Width="1024" VerticalAlignment="Center">
        <WrapPanel>
            <local:RequestListView />

        </WrapPanel>
    </StackPanel>
</Window>

then this RequestListView is an User Control:

<UserControl x:Class="...RequestFormView">
    <WrapPanel Name="Teste" Margin="20" Width="1000">
        <StackPanel Name="LeftPanel" Width="500">
            <WrapPanel>
                <Label Content="Nome do Pedido"/>
                <TextBox MinWidth="200" Text="{Binding Request.descRequest}"></TextBox>
            </WrapPanel>
            <StackPanel Name="LeftContainer">

            </StackPanel>
        </StackPanel>
        <StackPanel Name="RigthPanel" Width="500">
            <Label Content="Os campos marcados com * são obrigatórios."></Label>
            <Button Width="75" Height="26" Content="Save"/>
            <Button Width="75" Height="26" Content="Cancel"/>
        </StackPanel>
        <StackPanel Name="RightContainer">

        </StackPanel>
    </WrapPanel>
</UserControl>

Now on my RequestFormViewModel I want to access the "LeftContainer" and "RightContainer" panels. I'm trying:

StackPanel rightPanel = (StackPanel)Application.Current.MainWindow.FindName("RightContainer");~

But returns null. As far as I seen he can't "look" inside the control. How can I get those two panels?

Taterhead
  • 5,763
  • 4
  • 31
  • 40
Andre Roque
  • 503
  • 1
  • 9
  • 31
  • So, you are trying to access `StackPanel` inside `RequestFormViewModel`. . BLUNDER – Gopichandar Mar 11 '16 at 13:11
  • what are you trying to achieve with ? in WPF a view model should NEVER access the view directly, just expose properties for the view to display – Boo Mar 11 '16 at 13:11
  • 2
    @Gopichandar that comment was very helpfull. You're the best :) – Andre Roque Mar 11 '16 at 13:12
  • @Boo I'm trying to build the Form with code. Maybe I should do it in the xaml.cs class. Is that what you saying? – Andre Roque Mar 11 '16 at 13:13
  • no ! build your form the xaml , (usually form are mainly static, if it's dynamic , we'll see in a second time) then , the properties that need to be displayed / edited have to been bound to the viewModel – Boo Mar 11 '16 at 13:17
  • I need to build it dynamically because the fields that are needed to be created are stored in a DB, so it depends on the kind of Form I open – Andre Roque Mar 11 '16 at 13:18
  • whate are the types of fields ? all the same or mixed like some strings / some int / some date ? – Boo Mar 11 '16 at 13:22
  • The idea is like: **Form1**: Have 2 textbox and 1 CheckBox **Form2**: Have 1 TextBox and 3 CheckBox Something like that, where the names also change. – Andre Roque Mar 11 '16 at 13:23
  • your problem is very nice :) i would in that case use an expando object as item source (1 expando = 1 form) , then i would feed the expando with item inheritings DataTemplateSelector to display a textbox or a checkbos depending the type you want. you should also have a look at : http://social.technet.microsoft.com/wiki/contents/articles/22761.different-ways-to-dynamically-select-datatemplate-for-wpf-listview.aspx (it's not what you whant but the approach will be nearly the same) – Boo Mar 11 '16 at 13:38
  • This is known as the X/Y problem. Read this meta question and the accepted answer: http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem What you're doing is wrong. Absolutely wrong. You should [edit] your question and add the following at the *top* of your question: "I am trying to do [details of your task]. My attempt to accomplish this isn't working. Here's what I'm currently doing [your question as it currently stands]". Knowing what you **need to do** we can tell you how to do it correctly. –  Mar 11 '16 at 14:14
  • @AndreRoque See my answer. Hope that was helpful to you. Also, thanks for appreciation. . :) – Gopichandar Mar 11 '16 at 14:41

1 Answers1

0

Here is the Helper class that can help you.

UIHelper.cs

public static class UIHelper
{        
    public static DependencyObject FindChild(DependencyObject parent, string name)
    {
        // confirm parent and name are valid.
        if (parent == null || string.IsNullOrEmpty(name)) return null;

        if (parent is FrameworkElement && (parent as FrameworkElement).Name == name) return parent;

        DependencyObject result = null;

        if (parent is FrameworkElement) (parent as FrameworkElement).ApplyTemplate();

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            result = FindChild(child, name);
            if (result != null) break;
        }

        return result;
    }
}

Call it like this

StackPanel foundStackPanel = (StackPanel) UIHelper.FindChild(Application.Current.MainWindow, "RightContainer");

Reference: Find control in the visual tree

I hope this was helpful to you :)

Community
  • 1
  • 1
Gopichandar
  • 2,742
  • 2
  • 24
  • 54