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

2vuwiymt  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(119)

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

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>

fslejnso

fslejnso1#

你试过用

Height="*"

字符串

Width="*"


或者问题是文本框似乎不适应它的内容?

des4xlb0

des4xlb02#

试试看:

Height = "auto"
 Width = "auto"

字符串

相关问题