私はしばらくの間検索していて、このトピックに関する投稿がたくさんありますが、どれも私に正しい答えを与えていないようです。
フォームに次のようにデータグリッドを作成しました。
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Top" Width="860">
<DataGrid ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Name="DGI"
Height="700"
ItemsSource="{Binding}"
Background="LightGray" RowBackground="LightYellow" AlternatingRowBackground="LightBlue">
</DataGrid>
</StackPanel>
次に、コード内のデータを次のようにバインドします。
private void btnUpdateGridI_Click(object sender, RoutedEventArgs e)
{
DGI.DataContext = null;
IEnumerable<DataRow> query =
from punch in dspl.Tables[0].AsEnumerable()
where punch.Field<String>("TOS").Contains(cmbTOSI.SelectedItem.ToString()) &&
punch.Field<String>("BU").Contains(cmbBUI.SelectedItem.ToString()) &&
punch.Field<String>("CLOSED").Contains(cmbClosedI.SelectedItem.ToString()) &&
punch.Field<String>("CAT").Contains(cmbCATI.SelectedItem.ToString())
select punch;
try
{
DataTable boundTable = query.CopyToDataTable<DataRow>();
DGI.DataContext = boundTable;
lbltotalitemsI.Content = boundTable.Rows.Count.ToString() + " ITEMS";
DGI.Columns[6].MaxWidth = 350;
}
catch
{
MessageBox.Show("No data exists for the current selection.");
lbltotalitemsI.Content = "0 ITEMS";
DGI.DataContext = null;
}
}
6列目のテキストを折り返そうとしています。最大幅を定義することはできましたが、テキストを折り返すために、ほとんどのオンラインソースはデータグリッドでのTextBlockの使用を参照しています。
これを動的に行う簡単な方法はありますか?複数の列に対してこれを実行したいと思います。