简体   繁体   English

如何通过Presenter中的x:Name访问View元素?

[英]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": 在视图中,我具有一个带有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. 或者更好的方法是,不要这样做,并在视图中定义一些抽象属性,例如“ IsViewTabbed”或类似的东西,这些属性将使UI代码从presenter中抽象出来。

Found it: 找到了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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