WPF弹出窗口无法正确自动调整大小

时间:2015-07-21 11:42:47

标签: wpf popup height

我有一个应该显示错误的Popup。此弹出窗口呈现一个TextBlock,绑定到字段ErrorMessage。由于我的错误消息正确更新,绑定显然正常工作。但是,当消息太长时,弹出窗口的高度不会改变,并且错误消息的某些部分仍然是隐藏的。我确信WPF Popup会自动调整其大小以适应其内容,但在这种情况下,我似乎无法使其正常工作。

错误消息声明如下:

private String _errorMessage;
public String ErrorMessage
{
    get { return _errorMessage; }
    set
    {
        _errorMessage = value;
        OnPropertyChanged();
    }
}

其值在FindErrorInDates()函数中被更改:

public void FindErrorInDates()
{
    this.ErrorCount = 0;
    this.HasError = false;
    List<String> errors = new List<String>();
    if (this.OutwardDeparturePlannedDate >= this.OutwardArrivalPlannedDate)
    {
       this.ErrorCount += 1;
       errors.Add("Outward : Departure must be before Arrival");
    }

    if (this.ReturnDeparturePlannedDate >= this.ReturnArrivalPlannedDate)
    {
        this.ErrorCount += 1;
        errors.Add("Return : Departure must be before Arrival");
    }

    if (this.OutwardDeparturePlannedDate >= this.ReturnDeparturePlannedDate
                || this.OutwardDeparturePlannedDate >= this.ReturnArrivalPlannedDate
                || this.OutwardArrivalPlannedDate >= this.ReturnDeparturePlannedDate
                || this.OutwardArrivalPlannedDate >= this.ReturnArrivalPlannedDate)
    {
        this.ErrorCount += 1;
        errors.Add("Conflict between Outward Date and Return Date");
    }

    this.HasError = this.ErrorCount > 0;
    this.ErrorMessage = String.Join("\r\n", errors);
}

最后是Popup。我已尝试过各种属性HeightWidth。我似乎无法弄清楚如何使这个ErrorMessage正确地适合200宽度的弹出窗口。我错过了什么?

<Popup Height="Auto"  IsOpen="{Binding IsMouseOver, ElementName=ValidDepartureDate, Mode=OneWay}" PopupAnimation="None" Placement="Bottom" AllowsTransparency="True">
    <Border Height="Auto" Width="200" CornerRadius="2" Padding="5" Background="DarkRed" Visibility="{Binding DataContext.List.Presenter.JourneyResUtility.ErrorCount, Converter={StaticResource ZeroToVisibilityConverter}, RelativeSource={RelativeSource AncestorType=UserControl}}">
        <TextBlock Height="Auto"  Margin="5" Style="{StaticResource SmallFontStyle}" Text="{Binding DataContext.List.Presenter.JourneyResUtility.ErrorMessage, RelativeSource={RelativeSource AncestorType=UserControl}}" TextWrapping="Wrap"></TextBlock>
    </Border>
</Popup>

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用

Height="*" 

AND

Width="*"

或者文本框似乎没有适应其内容的问题?