简体   繁体   English

C# WPF - 控制 DataGrid ItemsSource 项目的列宽

[英]C# WPF - Controlling column width of DataGrid ItemsSource items

the issue is hard to explain but I will try my best.这个问题很难解释,但我会尽力而为。 This code is part of a proof-of-concept for a webscraping application I am working on - only recently started.这段代码是我正在开发的网络抓取应用程序概念验证的一部分 - 最近才开始。

I am attempting to display 2 columns of data using a DataGrid.我正在尝试使用 DataGrid 显示 2 列数据。 I am able to display the data by assigning an ObservableCollection<> to the DataGrid.ItemsSource (see image below), the issue I am getting is that I have no control over column properties.我可以通过将ObservableCollection<>分配给DataGrid.ItemsSource (见下图)来显示数据,我遇到的问题是我无法控制列属性。 I am very new to WPF and have tried to 'sort of' adopt MVVM.我对 WPF 很陌生,并试图“有点”采用 MVVM。

在此处输入图片说明

In the picture it shows 4 columns, both the 'Title' and 'Price' from the ItemsSource are the 2 large ones.在图片中它显示了 4 列,来自 ItemsSource 的“Title”和“Price”都是 2 大列。

private void ScrapeProductButton_OnClick(object sender, RoutedEventArgs e)
{
    string url = this.ProductToScrapeUrlTextBox.Text;
    scraper.ScrapeData(url);
    
    var entries = scraper.Entries;
    WebScrapedItems.ItemsSource = entries;
    
    WebScrapedItems.Columns.Add(new DataGridTextColumn(){Header = "Title",
        Width = new DataGridLength(0.8, DataGridLengthUnitType.Star)});
    WebScrapedItems.Columns.Add(new DataGridTextColumn(){Header = "Price",
        Width = new DataGridLength(0.2, DataGridLengthUnitType.Star)});
}

As seen in the code behind above, I tried to add 2 columns of the correct formatting assuming the contents of the ItemsSource would just do the same (obviously it isn't).如上面后面的代码所示,我尝试添加 2 列正确格式的内容,假设 ItemsSource 的内容会做同样的事情(显然不是)。 Turning AutoGeneratedColumns off in the XAML doesn't display the ItemsSource but does display the other columns correctly.在 XAML 中关闭 AutoGeneratedColumns 不会显示 ItemsSource,但会正确显示其他列。

<StackPanel>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Button Name="ScrapeProductButton" Grid.Column="0"
            Margin="10 10 10 10" Background="AntiqueWhite"
            Content="Scrape Website" FontWeight="Bold"
            Click="ScrapeProductButton_OnClick"/>
        <TextBox Name="ProductToScrapeUrlTextBox" Grid.Column="1"
            Margin="10" Padding="2"/>
    </Grid>

    <DataGrid Height="350" Width="740" Margin="10" Name="WebScrapedItems"
        VerticalAlignment="Center" HorizontalAlignment="Center"
        RowHeight="30" ColumnWidth="390" IsReadOnly="True"
        AutoGenerateColumns="True" FrozenColumnCount="2">
    </DataGrid>

</StackPanel>

My goal is to just display the contents of the ItemsSource objects but such that they are correctly formatted.我的目标是只显示 ItemsSource 对象的内容,但它们的格式正确。 I feel like binding could be a potential reason its not working properly, but again I am still new to WPF and haven't started reading up on it yet.我觉得绑定可能是它无法正常工作的潜在原因,但我还是 WPF 的新手,还没有开始阅读它。

Any help would be greatly appreciated, and any WPF advice, MVVM or anything would be great also.任何帮助将不胜感激,任何 WPF 建议、MVVM 或任何东西也会很棒。

Thanks!谢谢!

I fixed the issue through binding to the field of the object that I wanted to display within the column.我通过绑定到我想在列中显示的对象的字段来解决这个问题。 This mean I could remove the formatting the in ScrapeProductButton_OnClick method.这意味着我可以删除ScrapeProductButton_OnClick方法中的格式。

XAML code is here: XAML 代码在这里:

<StackPanel>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Button Name="ScrapeProductButton" Grid.Column="0" Margin="10 10 10 10" Background="AntiqueWhite" Content="Scrape Website" FontWeight="Bold" Click="ScrapeProductButton_OnClick"/>
        <TextBox Name="ProductToScrapeUrlTextBox" Grid.Column="1" Margin="10" Padding="2"/>
    </Grid>

        <DataGrid Name="WebScrapedItems" AutoGenerateColumns="False">
            <DataGrid.Columns >
                <DataGridTextColumn Header="Title" Binding="{Binding Title}" Width="3*"/>
                <DataGridTextColumn Header="Price" Binding="{Binding Price}" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>
</StackPanel>

I appreciate the time people spent on looking through the problem - sorry if I wasted your time.我感谢人们花时间查看问题 - 如果我浪费了你的时间,抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM