Issue in Popup Behavior

Aug 26, 2009 at 6:08 PM

Hi, guys,

I have a problem during refactoring my project.  I study RI and use its Popup Behavior in my project. The "Cancel" button works fine. But another button "Save", implemented in the Prism Command, doesnot work again. The code in the xaml is as follows:

        <Button Height="20" x:Name="btnSave" cmd:Click.Command="{Binding SaveCommand}" HorizontalAlignment="Right" Margin="0,0,118,14" VerticalAlignment="Bottom" Width="59" Content="Save"/>
        <Button Height="20" x:Name="btnCancel" Behaviors:ButtonBehaviors.CloseAncestorPopup="true" HorizontalAlignment="Right" Margin="0,0,40,14" VerticalAlignment="Bottom" Width="59" Content="Cancel"/>
Is there anything conflict in these two behaviors? An issue in RI? Please add a button in the RI "NewsReader.xaml" to simulate my situation, you can find the problem.
Aug 27, 2009 at 6:36 PM

Hi Jianqiang,

I have tried to reproduce your issue in the Silverlight 3 StockTrader RI from Prism with no success. One point of confusion might be if you are creating the command in the NewsReader presenter. As the Datacontext of the NewsReader view is an instance of the NewsArticle class, the command should be created there (this is not a good approach in a real application,  just to get it to work in the RI). So, since the command must be in the class that is set as the DataContext of the view I updated the NewsArticle class like this:

   public class NewsArticle
        public NewsArticle() 
            this.ClickMe = new DelegateCommand<string>(this.OnClickMe);
        public DateTime PublishedDate { get; set; }
        public string Title { get; set; }
        public string Body { get; set; }
        public string IconUri { get; set; }

        public DelegateCommand<string> ClickMe

        private void OnClickMe(string b) 
            //Logic here

The updated XAML of the NewsReader view is the following:

<UserControl x:Class="StockTraderRI.Modules.News.Article.NewsReader"
    Width="640" Height="480">
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <TextBlock Height="17.167" FontFamily="Trebuchet MS" FontSize="12" FontWeight="Normal" Foreground="#FF1B75BB" TextAlignment="Left" TextWrapping="Wrap" HorizontalAlignment="Left" Width="165.36" Text="{Binding Path=PublishedDate}"/>
            <TextBlock Height="31.563" FontFamily="Trebuchet MS" FontSize="16" FontWeight="Normal" Foreground="#FF1B75BB" TextAlignment="Left" TextWrapping="Wrap" Text="{Binding Path=Title}"/>
            <Button Content="Click Me" cal:Click.Command="{Binding ClickMe}"></Button>
      <ScrollViewer VerticalScrollBarVisibility="Auto" Margin="0,0,0,0" VerticalAlignment="Stretch" Grid.Row="1" Style="{StaticResource ScrollViewerStyle}" Padding="0,0,4,0">
            <TextBox BorderThickness="0,0,0,0" FontFamily="Trebuchet MS" Foreground="#FF808080" FontSize="16" Text="{Binding Path=Body}" TextAlignment="Left" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Padding="0,0,0,0" IsReadOnly="True" />

Please let me know if this helps.

Damian Schenkelman

Sep 3, 2009 at 7:29 AM
Edited Sep 3, 2009 at 7:47 AM


You are right.

But how to implement this scenario:

<Button Content="Click Me" cal:Click.Command="{Binding ClickMe}"></Button>

When the ClickMe command finish executing, I want to close this View, but I find no way to do it.

Sep 3, 2009 at 11:46 AM


Haha, I study RI implementation, and simulate some code, put it at the end of the click method, it works fine now!


            ButtonBase button = this.View.FindName("btnSave") as ButtonBase;
            var popup = TreeHelper.FindAncestor(button, d => d is Popup) as Popup;
            if (popup != null)
                popup.IsOpen = false;