2016-08-09 10 views
1

キーボードで私の頭を十分に叩いてください。今、私がやりたいすべては、それらのグループが大胆ヘッダ作るですリストビューを作成してスタイルを変更してください。

FCView.FCListView.ItemsSource = myItemsSouce; 
CollectionView view = CollectionViewSource.GetDefaultView(FCView.FCListView.ItemsSource) as CollectionView; 
PropertyGroupDescription gd = new PropertyGroupDescription("Root"); 
view.GroupDescriptions.Add(gd); 

Unstyled listview

:私は完全にこのように動作しますリストビューを持っています。 (冗長ようで、どちらか正常に動作していないもの)

Style myStyle = new Style(typeof(GroupItem));  
DataTemplate dt = new DataTemplate(); 
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(GroupItem)); 
spFactory.SetValue(GroupItem.FontWeightProperty, FontWeights.Bold); 
spFactory.SetValue(GroupItem.ForegroundProperty, new SolidColorBrush(Colors.Red)); 
dt.VisualTree = spFactory; 
GroupStyle groupStyle = new GroupStyle(); 
groupStyle.HeaderTemplate = dt; 
groupStyle.ContainerStyle = myStyle; 
FCListView.GroupStyle.Add(groupStyle); 

しかし、私はそれを再結合していない限り、これは私のGroupDescriptionを上書き:3時間後に、これは私が思い付くことができる最高です。 (スタイルは、グループヘッダの下に他のリストビュー項目や、同じように良い、ない)のために実際にそれが(ContentControlある)GroupItemを使用してあまり良くない

答えて

0

グループヘッダーのスタイルを設定する簡単な方法はありますあなたのヘッダーをデータ化します。あなたの場所で私は単純なTextBlockを使用します。

あなたのDataTemplateがバインドされていないという理由だけで、グループの説明が表示されないことがポイントです。あなたはGroupItemのコンテンツを持つグループ(すなわち、その説明)のNameをバインドすることができます。このように

Style myStyle = new Style(typeof(GroupItem));  
DataTemplate dt = new DataTemplate(); 
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(GroupItem)); 
spFactory.SetValue(GroupItem.FontWeightProperty, FontWeights.Bold); 
spFactory.SetValue(GroupItem.ForegroundProperty, new SolidColorBrush(Colors.Red)); 
// You missed next line 
spFactory.SetBinding(GroupItem.ContentProperty, new Binding("Name")); 
// 
dt.VisualTree = spFactory; 
GroupStyle groupStyle = new GroupStyle(); 
groupStyle.HeaderTemplate = dt; 
groupStyle.ContainerStyle = myStyle; 
FCListView.GroupStyle.Add(groupStyle); 

:だからコメント行を追加します。

テンプレートを作成する最も良い方法は、XAMLを使用することです。とにかく何らかの理由でコードを使用する必要がある場合、私は次のコードを使用します:

DataTemplate dt = new DataTemplate(); 
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(TextBlock)); 
spFactory.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold); 
spFactory.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.Red)); 
spFactory.SetBinding(TextBlock.TextProperty, new Binding("Name")); 
dt.VisualTree = spFactory; 
GroupStyle groupStyle = new GroupStyle(); 
groupStyle.HeaderTemplate = dt; 

FCListView.GroupStyle.Add(groupStyle); 

私はあなたを助けることを願っています。

関連する問題