prism – 使用交互请求的自定义弹出窗口的大小

我使用了自定义确认弹出窗口,这是XAML:

<Grid>
    <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TextBlock Text="{Binding Content}" TextWrapping="Wrap" Width="150"/>
    <StackPanel Orientation="Horizontal" Margin="6" Grid.Row="1">
            <Button x:Name="YesBtn" Width="100" Content="OK" Click="OnOk_Click"/>
            <Button x:Name="NoBtn" Width="100" Content="No" Click="OnNo_Click"/>
    </StackPanel>
</Grid>

这是代码隐藏:

public partial class CustomConfirmation : IInteractionRequestAware
{
    public CustomConfirmation()
    {
        InitializeComponent();
    }
    public IConfirmation Confirmation
    {
        get { return this.DataContext as IConfirmation; }
        set { this.DataContext = value; }
    }

    public string Title { get; set; }
    public bool Confirmed { get; set; }
    public INotification Notification { get; set; }
    public Action FinishInteraction { get; set; }
    private void OnOk_Click(object sender, RoutedEventArgs e)
    {
        if (FinishInteraction != null)
        {
            Confirmation.Confirmed= true;
            FinishInteraction();
        }
    }

    private void OnNo_Click(object sender, RoutedEventArgs e)
    {
        if (FinishInteraction != null)
        {
            Confirmation.Confirmed = false;
            FinishInteraction();
        }
    }
}

在视图模型类中,我有:

>两个命令(DispalyLongTextCommand和DispalyShortTextCommand):一个
显示长消息,另一个显示短消息
>我有InteractionRequest ConfirmationRequest
在ctor中初始化的对象以引发交互.

如果我首先显示长消息我的自定义窗口将其内容调整为空洞消息,那就没关系!
但如果想要显示短信,我的窗口会保持以前的大小!

注意:即使我将窗口SizeToContent样式设置为WidthAndHeight但它不起作用.

<ei:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding ConfirmationRequest, Mode=TwoWay}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
                <prism:PopupWindowAction.WindowStyle>
                    <Style TargetType="Window">
                        <Setter Property="SizeToContent" Value="WidthAndHeight"/>
                    </Style>
                </prism:PopupWindowAction.WindowStyle>
                <prism:PopupWindowAction.WindowContent>
                    <local:CustomConfirmation/>
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
    </ei:Interaction.Triggers>

《prism – 使用交互请求的自定义弹出窗口的大小》

《prism – 使用交互请求的自定义弹出窗口的大小》

你能指导我吗
提前致谢

解:
我通过在自定义弹出窗口的代码中添加此代码来解决问题,:

public CustomConfirmationView()
    {
        InitializeComponent();
        Loaded += CustomPopupView_Loaded;
    }

    private void CustomPopupView_Loaded(object sender, RoutedEventArgs e)
    {
        var parentWindow = this.Parent as Window;
        if (parentWindow != null)
        {
            parentWindow.Measure(parentWindow.DesiredSize);
        }
    }

最佳答案 每次显示新弹出窗口时都会重复使用WindowContent属性.因此,当您首次显示弹出窗口时,会显示CustomPopupView,并根据当前内容设置高度.现在,当您关闭弹出窗口并将内容更改为更大的消息然后再次显示时,CustomPopupView.Height已经由上一个操作设置,并且未及时更新以使新窗口获得正确的高度.因此,您现在必须调整窗口大小以匹配CustomPopupView高度的新大小.所以只需添加一些代码就可以在你的代码隐藏中处理这个:

    public CustomPopupView()
    {
        InitializeComponent();
        Loaded += CustomPopupView_Loaded;
    }

    private void CustomPopupView_Loaded(object sender, RoutedEventArgs e)
    {
        var parentWindow = this.Parent as Window;
        if (parentWindow != null)
            parentWindow.MinHeight = _txt.ActualHeight + 75;
    }

注意:’_ txt’是带有Content绑定的TextBlock的名称.

点赞