【问题标题】:SizeChanged Event Isn't FiringSizeChanged 事件未触发
【发布时间】:2018-06-12 17:30:35
【问题描述】:

所以,我最近一直在研究SizeChanged 事件,因为我希望我的字体大小按比例缩放,尽管我搜索了很多帖子,但没有一个对我有用 - 我的对象没有触发。

这是我的代码:

private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e) {
    welcomeTitleText.Text = "hi";
}

这甚至不像窗口大小那样使用,它只是改变文本,以确保它被触发。但是,由于某种原因它不起作用。有什么想法吗?

【问题讨论】:

  • 你是否也在 xaml 中分配了它?

标签: c# wpf resize screen-resolution


【解决方案1】:

确保您订阅了该活动。您必须在代码或 xaml 中注册事件的处理程序。

在代码示例中:

public MainWindow()
{

    // This call is required by the designer.
    InitializeComponent();

    // Add any initialization after the InitializeComponent() call.
    this.SizeChanged += MainWindow_SizeChanged;
}

或在 xaml 中:

<Window     
    x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"        
        mc:Ignorable="d"
    ...
    Loaded="AboutWindow_Loaded">
...

另请注意,如果 wpf 的后端认为更改太微不足道,则有时 sizeChanged 事件不会触发。 See the docs for more details.

提示,您可以尝试将文本包装在 Viewbox 中,这会将其自动调整为最大填充大小。然后你可以限制视图框(或它的容器)的大小(即最大宽度,最大高度)(我建议使用网格,并使用 *(星号)或 pt,more details here 查看列和行高)来实现合理的缩放。例如:

<Viewbox MaxHeight="50">
    <TextBlock>I'm Scaled Text</TextBlock>
</Viewbox>

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多