简体   繁体   English

更改菜单项上的Conent Control模板?

[英]Changing Conent Control template on Menu Item click?

Can you please tell me how to change content template when, in WPF, when it is clicked on different Menu Item headers. 在WPF中,单击不同的菜单项标题时,能否告诉我如何更改内容模板? I've defined user control that I can put it as a template. 我已经定义了用户控件,我可以把它作为模板。

For example: Menu Items are: Home, Players, Team . 例如:菜单项包括:主页,玩家,团队。 when i click on Home I want that specific template in my Content Control to pop up, tehen when I click on Players I want another template (list of players) to pop in Content Control as template. 当我点击Home时,我希望弹出我的Content Control中的特定模板,当我点击Players时,我希望另一个模板(播放器列表)以Content Control中的形式弹出作为模板。

How to do that with triggers in XAML? 如何在XAML中使用触发器?

Thank you very much :) 非常感谢你 :)

You can use a ContentControl to host whatever your content will be, and set the ContentControl.ContentTemplate based on how you want to draw your content. 您可以使用ContentControl来托管您的内容,并根据您希望绘制内容的方式设置ContentControl.ContentTemplate

As a very basic example, 作为一个非常基本的例子,

<ContentControl x:Name="MyContentControl">
    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding }" Value="Home">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <local:MyHomeUsercontrol />
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding }" Value="Players">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <local:MyPlayersUsercontrol />
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding }" Value="Team">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <local:MyTeamUsercontrol />
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
        </Style>
    </ContentControl.Style>
</ContentControl>

And on MenuItem.Click MenuItem.Click

MyContentControl.Content = "Home"; // or "Players" or "Team"

In this example I'm using a string for the ContentControl.Content , however if you were to use a class object such as a HomeViewModel or PlayersViewModel , your XAML could be simplified to use implicit data templates, which are templates that automatically get used whenever WPF tries to draw a specific class 在这个例子中,我使用ContentControl.Contentstring ,但是如果你要使用类对象,如HomeViewModelPlayersViewModel ,你的XAML可以简化为使用隐式数据模板,这些模板可以在任何时候自动使用WPF尝试绘制特定的类

<Window.Resources>
    <DataTemplate DataType="{x:Type HomeViewModel}">
        <local:MyHomeUserControl />
    </DataTemplate>
    <DataTemplate DataType="{x:Type PlayersViewModel}">
        <local:MyPlayersUserControl />
    </DataTemplate>
    <DataTemplate DataType="{x:Type TeamViewmModel}">
        <local:MyTeamUserControl />
    </DataTemplate>
</Window.Resources>

<ContentControl x:Name="MyContentControl" />

and

MyContentControl.Content = new HomeViewModel();

How about using a tab control with tab placement at the top? 如何在顶部使用带标签位置的标签控件? it would be much cleaner that way.. 这样会更清洁..

Edit; 编辑; Example: 例:

  <TabControl>
        <TabItem>
            <TabItem.Header>
                <TextBlock Text="Football header!"/>
            </TabItem.Header>
            <TabItem.Content>
                <Button Content="Push for football!"/>
            </TabItem.Content>
        </TabItem>
    </TabControl>

this might help also; 这也可能有所帮助; http://www.switchonthecode.com/tutorials/the-wpf-tab-control-inside-and-out http://www.switchonthecode.com/tutorials/the-wpf-tab-control-inside-and-out

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

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