【问题标题】:WPF - Call back when parent view change widthWPF - 父视图更改宽度时回调
【发布时间】:2017-07-15 01:20:48
【问题描述】:

我有一个带有Orientation="Horizon" 的ListView,这意味着DataTemplate 看起来像“列”而不是行。一切正常,当我有很多无法在屏幕上显示的列时,ListView 会显示滚动条。我为 DataTemplate 设置了MinWidth。我想要完成的是,如果我的source 有少于 3 个对象来扩展 Datatemplate 的宽度以适应 ListView 的屏幕。 我知道我可以使用以下方法获取窗口的当前宽度:

System.Windows.SystemParameters.PrimaryScreenWidth;
System.Windows.SystemParameters.PrimaryScreenHeight;

我需要的是一个回调方法,当窗口大小发生变化时要执行以更新 DataTemplates 宽度。 WPF 中是否存在此功能?有没有其他选择?

【问题讨论】:

标签: c# wpf visual-studio


【解决方案1】:

我需要的是一个回调方法,当窗口大小发生变化时要执行以更新 DataTemplates 宽度。 WPF 中是否存在此功能?

您可以为Window 或ListView 处理SizeChanged 事件。

只要元素的大小发生变化,就会调用此事件。然后,您可以在事件处理程序中更改列的宽度,例如:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        SizeChanged += (s, e) => 
        {
            GridView gv = listView.View as GridView;
            gv.Columns[0].Width = ...;
        };
    }
}

【讨论】:

    猜你喜欢
    • 2016-11-15
    • 1970-01-01
    • 2020-10-17
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2015-11-24
    • 2014-01-18
    相关资源
    最近更新 更多