【やりたいこと】
・県名が同一の行は結合したい
・市町村が「市」の場合は閉じた状態にしておき、
(+)(-)ボタンの押下で開閉して町名を表示したい
(Expanderコントロールで実現出来そうだと考えています)
【困っていること】
DataGridコントロールを利用してItemsSourceプロパティに
VMのObservableCollectionをバインドする方法を取るつもりでした。
しかし、調べた結果DataGridコントロールでセル結合は出来ず、
やれたとしても疑似的に結合しているように見せるだけでパフォーマンスも悪そうとのことでした。
一旦DataGirdを使うという案をリセットして、再度適切なコントロールについて検討をしている状態です。
【聞きたいこと】
DataGrid以外で何かおすすめのコントロールはございますか。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
これは調べたわけではなく単なる勘ですが、そこまで特殊なコントロールを作って公開している人はまずいないと思います。
自分で作らなければいけません。
かなりの労力が必要だと思いますが、それに見合うだけの必要性があるのかどうかから検討してください。
私が実装するのであれば、結合前のデータを保持する ObservableCollection<T> を直接バインドするのではなく、そこから木構造を作ってバインドします。
XML
1<Window x:Class="WpfApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800"> 9 <Window.DataContext> 10 <local:ViewModel/> 11 </Window.DataContext> 12 <Grid> 13 <DataGrid ItemsSource="{Binding Addresses}" AutoGenerateColumns="False"> 14 <DataGrid.Columns> 15 <DataGridTextColumn Header="県名" Binding="{Binding Pref}"/> 16 <DataGridTemplateColumn Header="市区町村"> 17 <DataGridTemplateColumn.CellTemplate> 18 <DataTemplate> 19 <ListView ItemsSource="{Binding Cities}"/> 20 </DataTemplate> 21 </DataGridTemplateColumn.CellTemplate> 22 </DataGridTemplateColumn> 23 </DataGrid.Columns> 24 </DataGrid> 25 </Grid> 26</Window>
C#
1using System.Collections.Generic; 2using System.Collections.ObjectModel; 3using System.ComponentModel; 4using System.Runtime.CompilerServices; 5 6namespace WpfApp1 7{ 8 public class NotifyPropertyChanged : INotifyPropertyChanged 9 { 10 public event PropertyChangedEventHandler PropertyChanged; 11 protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null) 12 { 13 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 14 } 15 protected bool SetProperty<T>(ref T field, T value, [CallerMemberName]string propertyName = null) 16 { 17 if (EqualityComparer<T>.Default.Equals(field, value)) return false; 18 field = value; 19 OnPropertyChanged(propertyName); 20 return true; 21 } 22 } 23 24 public class ViewModel : NotifyPropertyChanged 25 { 26 public ViewModel() 27 { 28 Addresses = new ObservableCollection<Address>() 29 { 30 new Address("香川県", "高松市", "丸亀市", "坂出市", "小豆郡"), 31 new Address("愛媛県", "松山市"), 32 new Address("高知県", "高知市"), 33 new Address("徳島県", "徳島市"), 34 }; 35 } 36 37 public ObservableCollection<Address> Addresses { get; private set; } 38 } 39 40 public class Address : NotifyPropertyChanged 41 { 42 public Address(string pref, params string[] cities) 43 { 44 Pref = pref; 45 foreach (var city in cities) Cities.Add(city); 46 } 47 48 private string pref; 49 public string Pref { get => pref; set => SetProperty(ref pref, value); } 50 51 public ObservableCollection<string> Cities { get; private set; } 52 = new ObservableCollection<string>(); 53 } 54}
投稿2018/12/08 15:28
総合スコア28660
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/10 14:49