WPF:GridViewColumn 调整大小事件

发布于 2024-08-20 12:20:33 字数 87 浏览 6 评论 0原文

我将 ListViewGridView 一起使用。是否有 GridViewColumn 调整大小事件?

I'm using ListView with GridView. Is there GridViewColumn resize event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦醒时光 2024-08-27 12:20:33

我将改为处理 PropertyChanged 事件。 Visual Studio 智能感知中看不到 PropertyChanged 事件,但您可以欺骗它:)

 GridViewColumn column = ...
 ((System.ComponentModel.INotifyPropertyChanged)column).PropertyChanged += (sender, e) =>
 {
     if (e.PropertyName == "ActualWidth")
     {
         //do something here...
     }
 };

I will handle the PropertyChanged event instead. The PropertyChanged event is not seen in the Visual Studio intellisense, but you can trick it :)

 GridViewColumn column = ...
 ((System.ComponentModel.INotifyPropertyChanged)column).PropertyChanged += (sender, e) =>
 {
     if (e.PropertyName == "ActualWidth")
     {
         //do something here...
     }
 };
千仐 2024-08-27 12:20:33

尽管 GridViewColumn 似乎没有 Resize 事件,但您可以绑定到 ColumnWidth 属性。

您可以使用下面的示例 XAML 来验证这一点 - 此示例不需要隐藏代码。它仅在一个方向上绑定,从列宽到文本框,当您调整大小时,您将看到文本框立即随列宽更新。

(这只是一个简单的示例;如果您想在代码中获取调整大小,我将创建一个具有 Width 属性的类,以便绑定可以在两个方向上工作)。

<StackPanel>
    <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn Width="{Binding ElementName=tbWidth1, Path=Text, Mode=OneWayToSource}"  />
                <GridViewColumn Width="{Binding ElementName=tbWidth2, Path=Text, Mode=OneWayToSource}"  />
            </GridView>
        </ListView.View>
        <ListViewItem>Item 1</ListViewItem>
        <ListViewItem>Item 2</ListViewItem>
    </ListView>
    <TextBox Name="tbWidth1" />
    <TextBox Name="tbWidth2" />
</StackPanel>

Although GridViewColumn does not appear to have a Resize event, you can bind to the ColumnWidth property.

You can verify this with sample XAML below - no code behind needed for this example. It binds only in one direction, from the column width to the text box, and when you resize you will see the textbox immediately update with the column width.

(This is just a simple example; if you want to pick up the resize in code I would create a class with a Width property so binding will work in both directions).

<StackPanel>
    <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn Width="{Binding ElementName=tbWidth1, Path=Text, Mode=OneWayToSource}"  />
                <GridViewColumn Width="{Binding ElementName=tbWidth2, Path=Text, Mode=OneWayToSource}"  />
            </GridView>
        </ListView.View>
        <ListViewItem>Item 1</ListViewItem>
        <ListViewItem>Item 2</ListViewItem>
    </ListView>
    <TextBox Name="tbWidth1" />
    <TextBox Name="tbWidth2" />
</StackPanel>
等往事风中吹 2024-08-27 12:20:33

Have a look at MSDN DridViewColumn details. It does not appaer to have such an event, probably some workaround required, I am not sure though. have look here

Hope it helps.

安静被遗忘 2024-08-27 12:20:33
private void ListView_Loaded( object sender, RoutedEventArgs e )
{
     // Add the handler to know when resizing a column is done
     ((ListView)sender).AddHandler( Thumb.DragCompletedEvent, new   DragCompletedEventHandler( ListViewHeader_DragCompleted ), true );
}

private void ListViewHeader_DragCompleted( object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e )
{
     ListView lv = sender as ListView;
    ... code handing the resize goes here ...
}

XAML:

<ListView Loaded="ListView_Loaded">
private void ListView_Loaded( object sender, RoutedEventArgs e )
{
     // Add the handler to know when resizing a column is done
     ((ListView)sender).AddHandler( Thumb.DragCompletedEvent, new   DragCompletedEventHandler( ListViewHeader_DragCompleted ), true );
}

private void ListViewHeader_DragCompleted( object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e )
{
     ListView lv = sender as ListView;
    ... code handing the resize goes here ...
}

XAML:

<ListView Loaded="ListView_Loaded">
酸甜透明夹心 2024-08-27 12:20:33

另一种方法:您可以将更改事件处理程序附加到 GridViewColumn Width 属性:(

PropertyDescriptor pd = DependencyPropertyDescriptor.FromProperty(
    GridViewColumn.WidthProperty, typeof(GridViewColumn));
GridView gv = (GridView)myListView.View;
foreach (GridViewColumn col in gv.Columns) {
    pd.AddValueChanged(col, ColumnWidthChanged);
}

...

private void ColumnWidthChanged(object sender, EventArgs e) { ... }

受答案启发 此处,有关 DataGrid 的类似问题。)

Another approach: you can attach a change event handler to the GridViewColumn Width property:

PropertyDescriptor pd = DependencyPropertyDescriptor.FromProperty(
    GridViewColumn.WidthProperty, typeof(GridViewColumn));
GridView gv = (GridView)myListView.View;
foreach (GridViewColumn col in gv.Columns) {
    pd.AddValueChanged(col, ColumnWidthChanged);
}

...

private void ColumnWidthChanged(object sender, EventArgs e) { ... }

(Inspired by an answer here, for a similar question about DataGrid.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文