Search code examples
c#win-universal-appuwpuwp-xamltemplate10

Access Popup-Dialog inside a data template


UI of Saavn App

I have just started learning windows app development. Like what do we call it (A dialog box, Contentdialogbox, Message Dialog)? Thanks in advance.

Okay I tried this since I have my data in a datatemplate inside a contentpresenter(Making a master detail view) now when user clicks on a icon the popup should open and also display the data related to that event selected inside that list.How do I achieve this since the popup dialog control is defined inside a datatemplate so in my cs file it does not recognize the control so I am not able to open the popup dialog.

Xaml Code:

  <DataTemplate x:Key="DetailContentTemplate" x:DataType="data:Event">
        <Grid>

            <Grid.RowDefinitions>
                <RowDefinition Height="200" />
                <RowDefinition Height="50" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />

            </Grid.RowDefinitions>
            <Grid x:Name="Section2" Grid.Row="0">
                <Grid.Background>
                    <ImageBrush ImageSource="ms-appx:///Assets/8.JPG" Stretch="Fill" />
                </Grid.Background>


                <TextBlock MaxWidth="250"
                           Margin="36,62,34,68"
                           FontFamily="Baskerville Old Face"
                           FontSize="30"
                           Foreground="{ThemeResource ToggleButtonPressedForegroundThemeBrush}"
                           TextWrapping="WrapWholeWords"
                           d:LayoutOverrides="Width, LeftPosition, RightPosition, TopPosition, BottomPosition">
                    <Run Text="Gravitas Premier League" />

                </TextBlock>

            </Grid>
            <Grid x:Name="Content"
                  Grid.Row="1"
                  Margin="0,10,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="0">
                    <RelativePanel>
                        <SymbolIcon x:Name="symbol"
                                    Margin="0,0,5,0"
                                    HorizontalAlignment="Left"
                                    RelativePanel.AlignLeftWithPanel="True"
                                    Symbol="Globe" />

                        <TextBlock HorizontalAlignment="Right"
                                   VerticalAlignment="Top"
                                   RelativePanel.RightOf="symbol"
                                   Style="{ThemeResource BaseTextBlockStyle}"
                                   Text="Category" />
                    </RelativePanel>
                </StackPanel>

                <StackPanel Grid.Column="1" HorizontalAlignment="Center">
                    <RelativePanel>
                        <SymbolIcon x:Name="symboll"
                                    Margin="0,0,5,0"
                                    HorizontalAlignment="Left"
                                    RelativePanel.AlignLeftWithPanel="True"
                                    Symbol="People" />

                        <TextBlock HorizontalAlignment="Right"
                                   VerticalAlignment="Top"
                                   RelativePanel.RightOf="symboll"
                                   Style="{ThemeResource BaseTextBlockStyle}"
                                   Text="SubCategory" />
                    </RelativePanel>
                </StackPanel>
                <StackPanel Grid.Column="2" HorizontalAlignment="Right">
                    <RelativePanel>
                        <SymbolIcon x:Name="symbllol"
                                    Margin="0,0,5,0"
                                    HorizontalAlignment="Left"
                                    RelativePanel.AlignLeftWithPanel="True"
                                    Symbol="Bullets" />

                        <TextBlock HorizontalAlignment="Right"
                                   VerticalAlignment="Top"
                                   RelativePanel.RightOf="symbllol"
                                   Style="{ThemeResource BaseTextBlockStyle}"
                                   Text="Rupee" />
                    </RelativePanel>
                </StackPanel>
            </Grid>
            <TextBlock Grid.Row="2"
                       HorizontalAlignment="Center"
                       Style="{ThemeResource ScenarioDescriptionTextStyle}"
                       Text="{x:Bind description}"
                       TextWrapping="WrapWholeWords" />

            <Grid Grid.Row="3">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>


                <SymbolIcon Grid.Column="0"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Symbol="Phone" />
                <SymbolIcon Grid.Column="1"
                            x:Name="People"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Symbol="People"
                            IsTapEnabled="True"
                            Tapped="ShowPopupOffsetClicked"
                            />



                <SymbolIcon Grid.Column="2"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Symbol="Mail" />

            </Grid>

        </Grid>
    </DataTemplate>

Now how do I open up the popup when the user taps the symbol with the name People and with necessary bindings of data with a observablecollection say EventList.


Solution

  • There are a lot of ways to implement the UI like in your screenshot. As you've added in your question, I suppose you are using Template10 in your project. And in Template10, we can use ModalDialog to implement this. Here I use a Minimal Template 10 project for example.

    Firstly, we may need to change ModalBackground to make the background color looks like what in your screenshot. As the ModalDialog we used here is the root frame created by Bootstrapper automatically, so we need override CreateRootElement in App.xaml.cs like:

    public override UIElement CreateRootElement(IActivatedEventArgs e)
    {
        var b = Current;
        var frame = new Windows.UI.Xaml.Controls.Frame();
        var nav = b.NavigationServiceFactory(BackButton.Attach, ExistingContent.Include, frame);
    
        //change background
        var background = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.Gray);
        background.Opacity = 0.2;
    
        return new Template10.Controls.ModalDialog
        {
            ModalBackground = background,
            DisableBackButtonWhenModal = true,
            Content = nav.Frame
        };
    }
    

    Then we can edit Busy.xaml to implement the panel in your screenshot. In Busy.xaml, it's a UserControl used as the ModalContent of ModalDialog. For example,

    <UserControl x:Class="T10Minimal.Views.Busy"
                 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:local="using:T10Minimal.Views"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 d:DesignHeight="300"
                 d:DesignWidth="400"
                 mc:Ignorable="d">
    
        <Grid Width="300"
              HorizontalAlignment="Center"
              VerticalAlignment="Center"
              Background="White"
              CornerRadius="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                ...
            </Grid.RowDefinitions>
            <TextBlock Margin="20,0"
                       VerticalAlignment="Center"
                       FontSize="24"
                       Foreground="Black">
                Song Options
            </TextBlock>
            <Button Margin="12"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Top"
                    Click="CloseClicked"
                    Foreground="Black"
                    Style="{StaticResource TextBlockButtonStyle}">
                <SymbolIcon Symbol="Clear" />
            </Button>
            ...
        </Grid>
    </UserControl>
    

    The bindings might like the BusyText in the original control, you can change its type to your binding data's type and also change the SetBusy method. After this, you can call SetBusy method in your ShowPopupOffsetClicked method to open the "popup".

    This is just a simple sample, you can refer to it to implement your own. And in the sample, I used the ModalDialog created as the root frame of the application. If you need more than one ModalDialog, you can refer to Search (and Login) Sample on GitHub.