我使用了一个自定义确认弹出窗口,这是 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();
}
}
}
在 View 模型类中,我有:
显示长消息,另一个显示短消息
在 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>
你能指导我吗
提前致谢
解决方案:
我通过在自定义弹出窗口后面的代码中添加此代码来解决问题:
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 已由上一个操作设置,并且不会及时更新以使新窗口获得正确的高度.因此,您现在必须调整 Window 的大小以匹配 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”是具有内容绑定(bind)的 TextBlock 的名称。
关于prism - 使用交互请求的自定义弹出窗口的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33805018/