简体   繁体   English

试图在代码隐藏中包装数据网格文本

[英]Trying to wrap datagrid text in code-behind

Using C# and WPF, I am trying to wrap the text in a DataGrid Column. 使用C#和WPF,我试图将文本包装在DataGrid列中。 I am setting the ItemsSource of the DataGrid in the code-behind, rather than using data binding. 我在后面的代码中设置了DataGrid的ItemsSource,而不是使用数据绑定。

As far as I can tell, there isn't a 'wrap' property for a datagrid, but it is possible to nest cell data inside of a textblock that can be wrapped. 据我所知,数据网格没有'wrap'属性,但是可以将单元格数据嵌套在可以包装的文本块中。 Unfortunately everything I've seen to do this uses data binding in the xaml, which would require a lot of refactoring, so I am trying to avoid it. 不幸的是,我见过的所有操作都在xaml中使用了数据绑定,这将需要大量的重构,所以我试图避免这种情况。

My xaml item: 我的xaml项目:

<DataGrid x:Name="dgvOrderItems" AutoGeneratedColumns="DgvOrderItems_AutoGeneratedColumns" Height="570" VerticalAlignment="Stretch" Width="auto" HorizontalAlignment="Stretch" FontSize="10" Padding="0" Margin="0,20,0,0" ></DataGrid>

My code behind for the DgvOrderItems_AutoGeneratedColumns: 我在DgvOrderItems_AutoGeneratedColumns后面的代码:

dgvOrderItems.SelectionUnit = DataGridSelectionUnit.FullRow;
dgvOrderItems.IsReadOnly = true;
dgvOrderItems.ColumnHeaderHeight = 15;
dgvOrderItems.Columns[0].Header = "Order Item";
dgvOrderItems.Columns[0].Width = 113;
dgvOrderItems.Columns[1].Visibility = Visibility.Hidden;
dgvOrderItems.Columns[2].Header = "Qty.";
dgvOrderItems.Columns[2].Width = 25;

As yet, I have been unable to find a good way to wrap the "Order Item" if the length exceeds the specified width. 到目前为止,如果长度超过指定的宽度,我还找不到一种好的方法来包装“订购商品”。

After going back and forth with it, I decided to go with what @TerryTyson and @EdPlunkett suggested by refactoring it with better data binding. 反复研究之后,我决定采用更好的数据绑定重构@TerryTyson和@EdPlunkett的建议。 Thanks guys for the advice. 谢谢你们的建议。

XAML: XAML:

<DataGrid x:Name="dgvOrderItems" Height="570" VerticalAlignment="Stretch" Width="138" HorizontalAlignment="Stretch" FontSize="10" Padding="0" HorizontalScrollBarVisibility="Hidden">
<DataGrid.ColumnHeaderHeight>20</DataGrid.ColumnHeaderHeight>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Order Item" Width="108" Binding="{Binding ItemNumber}">
            <DataGridTextColumn.ElementStyle>
                <Style>
                    <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="Qty." Width="30" Binding="{Binding ItemQty}">
            <DataGridTextColumn.ElementStyle>
                <Style>
                    <Setter Property="TextBlock.TextAlignment" Value="Center" />
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

Class for my data: 我的资料类别:

public class SimpleOrderInfo
{
    public string ItemNumber { get; set; }
    public int ItemQty { get; set; }

    public SimpleOrderInfo(string itemNumber, int itemQty)
    {
        this.ItemNumber = itemNumber;
        this.ItemQty = itemQty;
    }
}

Setting my ItemsSource: 设置我的ItemsSource:

public List<SimpleOrderInfo> simpleOrderInfo = new List<SimpleOrderInfo>();
simpleOrderInfo = business.GetSimpleOrderInfo(orderNumber);
dgvOrderItems.ItemsSource = simpleOrderInfo;

I feel like this could still probably be cleaned up some, but it is working, and is much cleaner than what I started with. 我觉得这可能仍然可以清除一些,但是它正在工作,并且比我刚开始使用时要干净得多。

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

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