Search code examples
xamlmvvmpopupitemscontrol

Placing PopUp, which is inside a Itemscontrol


I've got an itemscontrol, in which all my elements are PopUps. The problem is I do not know how to place them compared to the grid in which the itemscontrol is placed. I've tried using horizontal- and vertical alignment but this does not help. The xaml code I've got so far is:

<Grid>     
<ItemsControl Grid.Row="1" VerticalContentAlignment="Top" HorizontalAlignment="Left" ItemsSource="{Binding PopUp}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                        <ViewModel:PopUpTemplateSelector.EndTurnMenuTemplate>
                            <DataTemplate>
                                <Popup IsOpen="{Binding PU.IsOpen}" HorizontalAlignment="Left" VerticalAlignment="Top">
                                    <Design:InGame_NextTurn_UserControl/>
                                </Popup>
                            </DataTemplate>
                        </ViewModel:PopUpTemplateSelector.EndTurnMenuTemplate>
                    </ViewModel:PopUpTemplateSelector>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
</Grid>

Solution

  • I would suggest maybe using the following overlay method instead.

    From this Nokia page:

    Overlay XMAL:

    <UserControl x:Class="WindowsPhoneSample7.OverLay"
        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"
        mc:Ignorable="d"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        d:DesignHeight="800" d:DesignWidth="480">
    
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="400"/>
                <RowDefinition Height="400"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="1">
                <ProgressBar IsIndeterminate="True" Foreground="Orange" Height="50" Width="480" VerticalAlignment="Center"/>
                <TextBlock Text="Wait" Foreground="Orange" HorizontalAlignment="Center"/>
            </StackPanel>
        </Grid>
    </UserControl>
    

    Overlay code-behind:

    public OverLay()
    {
         InitializeComponent();
         this.LayoutRoot.Height = Application.Current.Host.Content.ActualHeight;
         this.LayoutRoot.Width = Application.Current.Host.Content.ActualWidth;   
         SystemTray.IsVisible = false; //to hide system tray
    }
    

    MainPage code-behind:

    public partial class MainPage : PhoneApplicationPage
    {
        private Popup popup;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.popup = new Popup();
        }
    
        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            this.LayoutRoot.Opacity = 0.2;
            OverLay ovr = new OverLay();
            this.popup.Child = ovr;
            this.popup.IsOpen = true;
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (s, a) =>
            {
                Thread.Sleep(5000);
            };
            worker.RunWorkerCompleted += (s, a) =>  
            {
                popup.IsOpen = false;
                this.LayoutRoot.Opacity = 1.0;
            };
            worker.RunWorkerAsync();
        }
    
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            this.popup.IsOpen = false;
        }
    }