简体   繁体   English

XAML运行时用户控件绑定

[英]XAML Runtime Usercontrol Binding

I have been fixating on this from some time: I am developing a windows phone app: I have a XAML Page as a template and three UserControls: 一段时间以来,我一直在解决此问题:我正在开发Windows Phone应用程序:我有一个XAML页面作为模板和三个UserControl:

One of which has map-layout, one generalInfo-layout, Summary+Pic layout. 其中之一具有地图布局,一个具有通用信息布局,即摘要+图片布局。

I want to create 3 buttons at the top and change the active UserControl respectively. 我想在顶部创建3个按钮,并分别更改活动的UserControl。

I dont want to you use a PivotPage. 我不想您使用PivotPage。

HELP? 救命? Advice? 忠告? Code? 码?

One very primitive approach would be to subscribe to the Click event of all 3 buttons and basically just change the Visibility property of your UserControls in the event handlers appropriately. 一种非常原始的方法是订阅所有3个按钮的Click事件,并且基本上只是在事件处理程序中适当地更改UserControls的Visibility属性。

Of course there are other approaches you might want to consider - like using the MVVM pattern or using TabControl and TabItem from the sdk if available for WP7 or using event triggers - but this can be a good starting point. 当然,您可能还需要考虑其他方法-例如使用MVVM模式或使用sdk中的TabControl和TabItem(如果可用于WP7)或使用事件触发器-但这是一个不错的起点。

Your MainPage should look something like this: 您的MainPage应该看起来像这样:

<UserControl x:Class="SilverlightApplication10.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:silverlightApplication10="clr-namespace:SilverlightApplication10"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <StackPanel x:Name="LayoutRoot"
                    Orientation="Horizontal"
                    Background="White">

            <Button Height="30"
                    Content="content1"
                    Click="Button_Click" />

            <Button Height="30"
                    Content="content2"
                    Click="Button_Click_1" />

            <Button Height="30"
                    Content="content3"
                    Click="Button_Click_2" />

        </StackPanel>

        <Grid Grid.Row="1">
            <silverlightApplication10:SilverlightControl1 x:Name="ctrl1"
                                                          Visibility="Collapsed" />

            <silverlightApplication10:SilverlightControl2 x:Name="ctrl2"
                                                          Visibility="Collapsed" />

            <silverlightApplication10:SilverlightControl3 x:Name="ctrl3"
                                                          Visibility="Collapsed" />
        </Grid>

    </Grid>
</UserControl>

And then your event handlers should take care of setting the Visibility property of all these UserControls: 然后,事件处理程序应注意设置所有这些UserControls的Visibility属性:

private void Button_Click(object sender, RoutedEventArgs e)
{
    ctrl1.Visibility = Visibility.Visible;
    ctrl2.Visibility = Visibility.Collapsed;
    ctrl3.Visibility = Visibility.Collapsed;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    ctrl2.Visibility = Visibility.Visible;
    ctrl1.Visibility = Visibility.Collapsed;
    ctrl3.Visibility = Visibility.Collapsed;
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    ctrl3.Visibility = Visibility.Visible;
    ctrl1.Visibility = Visibility.Collapsed;
    ctrl2.Visibility = Visibility.Collapsed;
}

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

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