添加项目时调整数据绑定 ListViewColumns 的大小

发布于 2024-12-19 17:09:50 字数 1191 浏览 1 评论 0原文

我正在制作一个具有数据绑定 ListView 的 WPF 应用程序。加载应用程序时,ListViewColumns 的宽度将自动调整大小,但添加或更改项目时不会自动调整大小。我尝试刷新列表视图,在 xaml 和 VB 代码中将列的宽度设置为 auto、-1 或 -2,并且在用项目重新填充之前尝试将 itemssource 更改为空。这是 xaml 代码:

<ListView x:Name="lsvPersons" Margin="5,5,5,35" ItemsSource="{Binding Persons}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
            <GridViewColumn DisplayMemberBinding="{Binding Gender}" Header="Gender"/>
            <GridViewColumn DisplayMemberBinding="{Binding Age}" Header="Age"/>
        </GridView>
    </ListView.View>
</ListView>
<Button x:Name="btnAddPerson" Content="Add" Height="25" Margin="0,0,200,5" Width="80"/>

绑定与控制器一起使用,控制器从 SQL 数据库获取具有 Person.getPersons 的人员:

Private oController As New MainController()
Public Sub New()
    MyBase.New()
    Me.InitializeComponent()
    Me.DataContext = oController
End Sub

按下按钮后,将打开一个窗口以添加人员。窗口关闭后,使用以下代码将项目添加到列表视图中,从而刷新列表视图中的项目:

lsvPersons.ItemsSource = Person.getPersons()

那么,当添加或编辑项目时,我需要做什么来自动调整列表视图的列大小?

I'm making an WPF-application that has a databound ListView. When loading the application, the width of the ListViewColumns will auto-resize, but when adding or changing an item it doesn't auto-resize. I've tried refreshing the listview, setting the width of the column to auto, -1 or -2 in both xaml and VB-code and I've tries changing the itemssource to nothing before refilling it with items. This is the xaml code:

<ListView x:Name="lsvPersons" Margin="5,5,5,35" ItemsSource="{Binding Persons}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
            <GridViewColumn DisplayMemberBinding="{Binding Gender}" Header="Gender"/>
            <GridViewColumn DisplayMemberBinding="{Binding Age}" Header="Age"/>
        </GridView>
    </ListView.View>
</ListView>
<Button x:Name="btnAddPerson" Content="Add" Height="25" Margin="0,0,200,5" Width="80"/>

The binding works with a controller, which gets the persons with Person.getPersons from a SQL-database:

Private oController As New MainController()
Public Sub New()
    MyBase.New()
    Me.InitializeComponent()
    Me.DataContext = oController
End Sub

After pressing the button, a window will open to add a person. After the window is closed, the item is added to the listview with following code, which refreshes the items in the listview:

lsvPersons.ItemsSource = Person.getPersons()

So, what do I need to do to auto-resize the columns of the listview when an item is added or editted?

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

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

发布评论

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

评论(1

芯好空 2024-12-26 17:09:50
        GridView gv = lvSrchResulsGrid.View as GridView;
        if (gv != null)
        {
            int colNum = 0;
            foreach (GridViewColumn c in gv.Columns)
            {
                // Code below was found in GridViewColumnHeader.OnGripperDoubleClicked() event handler (using Reflector)
                // i.e. it is the same code that is executed when the gripper is double clicked
                // if (adjustAllColumns || App.StaticGabeLib.FieldDefsGrid[colNum].DispGrid)
                // if (adjustAllColumns || fdGridSorted[colNum].AppliedDispGrid)
                // {
                    if (double.IsNaN(c.Width))
                    {
                        c.Width = c.ActualWidth;
                    }
                    c.Width = double.NaN;
                // }
            }
        }
        GridView gv = lvSrchResulsGrid.View as GridView;
        if (gv != null)
        {
            int colNum = 0;
            foreach (GridViewColumn c in gv.Columns)
            {
                // Code below was found in GridViewColumnHeader.OnGripperDoubleClicked() event handler (using Reflector)
                // i.e. it is the same code that is executed when the gripper is double clicked
                // if (adjustAllColumns || App.StaticGabeLib.FieldDefsGrid[colNum].DispGrid)
                // if (adjustAllColumns || fdGridSorted[colNum].AppliedDispGrid)
                // {
                    if (double.IsNaN(c.Width))
                    {
                        c.Width = c.ActualWidth;
                    }
                    c.Width = double.NaN;
                // }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文