如何在WPF + prism中使用InteractionRequest显示弹出窗口绑定到窗口的属性?

时间:2018-01-27 11:40:02

标签: wpf binding prism

我正在使用WPF和Prism创建一个应用程序。我的目的是在按钮点击时显示一个弹出窗口。我正在使用交互请求来实现相同的目标。我不想显示默认窗口标题,但想在弹出窗口使用的视图内的控件中显示标题。我尝试使用绑定'RelaviteSource'设置RelativeSourceType = Window。但是,即使VS2017显示的可视树包含一个Window,绑定也不起作用。

MainView.xaml

<UserControl x:Class="PopupTests.Views.MainView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:prism="http://prismlibrary.com/"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:PopupTests"
         prism:ViewModelLocator.AutoWireViewModel="True"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding ShowPopupRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
                <prism:PopupWindowAction.WindowContent>
                    <local:PopupView/>
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>
    <Grid >
        <Button Margin="20" Content="Click Here" Command="{Binding OnClick}"/>
    </Grid>
</UserControl>

MainViewModel.CS

using Prism.Commands;
using Prism.Interactivity.InteractionRequest;
using Prism.Mvvm;
using System.Windows.Input;

namespace PopupTests.ViewModels
{
    public class MainViewModel : BindableBase
    {
        public ICommand OnClick { get; }
        public InteractionRequest<INotification> ShowPopupRequest { get; } = new InteractionRequest<INotification>();
        public MainViewModel()
        {
            OnClick = new DelegateCommand(OnClicked);
        }

        private void OnClicked()
        {
            var notification = new Notification() { Title = "Test Titile", Content = null };
            ShowPopupRequest.Raise(notification);
        }
    }
}

PopupView

<UserControl x:Class="PopupTests.PopupView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Name="blk" HorizontalAlignment="Left" Margin="47,105,0,0" TextWrapping="Wrap" 
                   Text="{Binding Path=Title, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
                   VerticalAlignment="Top" Height="32" Width="151"/>

    </Grid>
</UserControl>

我希望窗口显示的标题“测试标题”也会显示在文本框中。但不幸的是绑定不起作用,我收到如下跟踪消息

无法找到与引用'RelativeSource FindAncestor,AncestorType ='System.Windows.Window',AncestorLevel ='1'的绑定源。 BindingExpression:路径=标题;的DataItem = NULL; target元素是'TextBlock'(Name =''); target属性是'Text'(类型'String')

我在这里缺少什么?

更新

经过一些试验和错误之后,我觉得这是由于一些时间问题。如果我在'Loaded'事件中添加以下代码,则值为appering

        var binding = blk.GetBindingExpression(TextBlock.TextProperty);
        blk.SetBinding(TextBlock.TextProperty, binding.ParentBinding);

但我不想让代码落后,而是希望只使用xaml解决方案。任何帮助表示赞赏。

0 个答案:

没有答案