简体   繁体   中英

How can I access my View elements via x:Name from my Presenter?

My presenter defines its own view :

public SmartFormPresenter(SmartFormView view)
{
    View = view;
    View.DataContext = this;
}

In the view I have an element with x:Name="MainTabControl":

<DockPanel LastChildFill="True">
    <TabControl x:Name="MainTabControl" DockPanel.Dock="Top" ItemsSource="{Binding SmartFormAreaPresenters}">
        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Header" Value="{Binding Title}"/>
            </Style>...

How can I access this element as I do in code behind, something like this:

PSEUDO-CODE:

View.Root.Children.MainTabControl.Visibility = Visibility.Collapsed;

You can define a public property in your view that will expose the private field. Or better, don't do it and define some abstract property in your view, like "IsViewTabbed" or sth like this, that will abstract UI code out of presenter.

Found it:

TabControl mainTabControl = View.FindName("MainTabControl") as TabControl;
mainTabControl.Visibility = Visibility.Hidden;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM