views:

45

answers:

0

Hi, I am trying to create an app where I am going to have 3 menus in multiple pages. Each menu will have uniques menu items. I need to instantiate these menus to multiple pages since all 3 menus will be the same in all pages. I created 3 Lists for 3 sets of RoutedUICommands for each menu in the App level which should be accessible by any class. I can pass text strings to all menu items but for some reason I cannot pass commands associated with each menu item. For some reason, CommandBindings cannot be added in App level. I am missing something here. I am wondering if someone has knowledge on how to make it work. Thank you in advance.

C# in App.xaml.cs:

namespace DynamicMenu { public partial class App : Application, IFoo {
    public List<RoutedUICommand> commands1 = new List<RoutedUICommand>();
    public List<RoutedUICommand> commands2 = new List<RoutedUICommand>();
    public List<RoutedUICommand> commands3 = new List<RoutedUICommand>();


    // Container 1 
    public static RoutedUICommand NameCommand = new RoutedUICommand("Name2", "NameCommand", typeof(App));
    public static RoutedUICommand StreetCommand = new RoutedUICommand("Street", "StreetCommand", typeof(App));
    public static RoutedUICommand GroupCommand = new RoutedUICommand("Add to Group", "AddGroup", typeof(App));

    // Container 2 
    public static RoutedUICommand ViewDetailsCommand = new RoutedUICommand("View Details", "ViewDetailsCommand", typeof(App));

    // Container 3 
    public static RoutedUICommand StartCommand = new RoutedUICommand("Start", "StartCommand", typeof(App));
    public static RoutedUICommand StopCommand = new RoutedUICommand("Stop", "StopCommand", typeof(App));
    public static RoutedUICommand LoadCommand = new RoutedUICommand("Load", "LoadCommand", typeof(App));


    public App()
    {
        this.InitializeComponent();

        Commands1.Add(NameCommand);
        Commands1.Add(StreetCommand);
        Commands1.Add(GroupCommand);

        Commands2.Add(ViewDetailsCommand);

        Commands3.Add(StartCommand);
        Commands3.Add(StopCommand);
        Commands3.Add(LoadCommand);

       this.CommandBindings.Add(new CommandBinding(NameCommand, (o, e) => { MessageBox.Show("Test 1"); }, (o, e) => { e.CanExecute = true; }));
    }


    public List<RoutedUICommand> Commands1
    {
        get
        {
            return this.commands1;
        }

    }

    public List<RoutedUICommand> Commands2
    {
        get
        {
            return this.commands2;
        }
    }

    public List<RoutedUICommand> Commands3
    {
        get
        {
            return this.commands3;
        }
    }
}

}

C# interface class:

namespace DynamicMenu { interface IFoo
{
    List<RoutedUICommand> Commands1 {get;}
    List<RoutedUICommand> Commands2 {get;}
    List<RoutedUICommand> Commands3 {get;}
}

}

Page1.xaml.cs

namespace DynamicMenu {
public partial class Page1
{
    public Page1()
    {
        this.InitializeComponent();
        IFoo currentApp = (IFoo)App.Current;
        Container1.ItemsSource = currentApp.Commands1;
        Container2.ItemsSource = currentApp.Commands2;
        Container3.ItemsSource = currentApp.Commands3;
    }
}

}

Page XAML

<Page.Resources>

    <Style x:Key="ListViewStyleTask" TargetType="{x:Type ListView}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListView}">
                    <Grid>
                        <Menu x:Name="mainMenu">
                            <MenuItem x:Name="menuItem" Header="Tasks" ItemsSource="{TemplateBinding ItemsSource}" >
                                <MenuItem.ItemContainerStyle>
                                    <Style TargetType="{x:Type MenuItem}">
                                        <Setter Property="Command" Value="{Binding}" />
                                        <Setter Property="Header" Value="{Binding Path=Text, RelativeSource={RelativeSource Self}}" />
                                        <Setter Property="CommandTarget" Value="{Binding RelativeSource={RelativeSource Self}}"/>


                                    </Style>
                                </MenuItem.ItemContainerStyle>
                            </MenuItem>
                        </Menu>
                    </Grid>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

<Grid x:Name="LayoutRoot">
    <ListView x:Name="Container1" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Style="{DynamicResource ListViewStyleTask}">
        <ListView.View>
            <GridView>
                <GridViewColumn/>
            </GridView>
        </ListView.View>
    </ListView>
    <ListView x:Name="Container2" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Style="{DynamicResource ListViewStyleTask}" Margin="113,0,0,0">
        <ListView.View>
            <GridView>
                <GridViewColumn/>
            </GridView>
        </ListView.View>
    </ListView>
    <ListView x:Name="Container3" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Style="{DynamicResource ListViewStyleTask}" Margin="225,0,0,0">
        <ListView.View>
            <GridView>
                <GridViewColumn/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>