MVVM-Pattern を使用して WPF プロジェクトに取り組んでいます。
今まではすべて正常に動作していましたが、今は醜いデザインの問題に直面しています。私のデータを表示するには、他の人と同じように を使用しますが、を介してオブジェクト全体を表示する必要があるためDataGrid
、 myはです。私はこれも機能しますが、いくつかの UI エントリの後に存在しない列を並べ替える必要がありますが、これは面倒なので、これらのオブジェクトを提供するために使用するテーブルのようなクラスの種類を知りたいですか?ItemsSource
List<List<MyClass>>
DataTemplate
DatagridCells
私ができる必要があること(簡単にプログラムできるようにするため):
- 各セルにアクセスする
- 列の並べ替え/入れ替え
- 行の並べ替え/入れ替え
編集:
ここで、私が実際に何をしているのかを理解するのはそれほど簡単ではないことがわかりました。
彼らはそれが何であるかを理解していないので、List<List<MyClass>>
それは単なるMyClass[,]
@Charleh
"How do you reorder non-existent columns?"
素晴らしい質問...正しく読むと、新しいタイプのデータプロバイダー、つまり実際のテーブルが必要であり、List<List<MyClass>>
列を持たない私のものではないことがわかりList<List<MyClass>>
ます MyListLister
。セルになるMyListLister[0]
MyListLister[0][0]
You mean you have a single column because you are using a data template to show the object?
コードごとに DataGrid を作成します。動的にする必要があるためです。これは、ユーザーが各選択後にコンボボックスの選択に応じて DataGrid 全体のスタイルを変更できるためです。DataGrid を再作成して MyListLister を変更する必要がありますが、ItemsSoucre として使用するには、このハックを行う
@HighCore
Post the relevant XAML
実際には有用な xaml がいくつかあるわけではないので、少し休んでから簡単な例を提供します
編集2:
例が遅れて申し訳ありませんが、ここに私の簡単な例を示します
メインウィンドウ
XAML
<Window x:Class="TableToDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Label Name="theElement"/>
</Window>
.CS
private void Window_Loaded(object sender, RoutedEventArgs e)
{
theElement.Content = new MyUsercontrol();
}
マイユーザーコントロール
XAML
<UserControl x:Class="TableToDataGrid.MyUsercontrol"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vmv="clr-namespace:TableToDataGrid">
<UserControl.Resources>
<DataTemplate x:Key="MyCellTemplate" DataType="{x:Type vmv:myClass}">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</UserControl.Resources>
<Label Name="AddYourDataGridHere"/>
</UserControl>
.CS
using System.Linq;
using System.Windows;
using System.Collections;
using System.Windows.Data;
using System.Windows.Controls;
using System.Collections.Generic;
namespace TableToDataGrid
{
/// <summary>
/// Interaktionslogik für MyUsercontrol.xaml
/// </summary>
public partial class MyUsercontrol : UserControl
{
DataGrid myDataGrid = new DataGrid();
public MyUsercontrol()
{
InitializeComponent();
var list = new List<List<myClass>>();
for (int row = 0; row < 3; row++)
{
var myRow = new List<myClass>();
for (int col = 0; col < 5; col++)
myRow.Add(new myClass() { ID = col, Name = "Row" + row + " Column:" + col });
list.Add(myRow);
}
#region the hack
for (int c = 0; c < 5; c++)
{
DataGridTemplateColumn column = new DataGridTemplateColumn();
var factory = new FrameworkElementFactory(typeof(ContentPresenter));
factory.SetBinding(ContentPresenter.ContentProperty, new Binding(string.Format("[{0}]", c.ToString())));
factory.SetValue(ContentPresenter.ContentTemplateProperty, this.FindResource("MyCellTemplate") as DataTemplate);
column.SetValue(DataGridTemplateColumn.CellTemplateProperty, new DataTemplate { VisualTree = factory });
myDataGrid.Columns.Add(column);
}
#endregion the hack
myDataGrid.ItemsSource = list.AsEnumerable<IEnumerable>();
myDataGrid.AutoGenerateColumns = false;
AddYourDataGridHere.Content = myDataGrid;
}
}
}
myClass.cs
namespace TableToDataGrid
{
public class myClass
{
public int ID { get; set; }
public string Name { get; set; }
}
}
ご覧のとおり、結果は通常のテーブルです。そして、同じ結果をもっと簡単な方法で提供する方法を知っている人はいItemsSource
ますか?