Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
WPF勉強会 第3回 コレクションへのデータバインディング/WPFStudy3
Search
hachiilcane
March 01, 2022
Programming
0
100
WPF勉強会 第3回 コレクションへのデータバインディング/WPFStudy3
WPF勉強会 第3回 コレクションへのデータバインディング
説明後に実演するライブコーディングのコードはこちら。
https://github.com/hachiilcane/WpfStudy
hachiilcane
March 01, 2022
Tweet
Share
More Decks by hachiilcane
See All by hachiilcane
機械学習勉強会01 1次関数での回帰分析/MLStudy01
hachiilcane
0
60
機械学習勉強会02 多項式近似と最小二乗法による推定/MLStudy02
hachiilcane
0
77
機械学習勉強会03 最急降下法/MLStudy03
hachiilcane
0
37
機械学習勉強会04 偏微分と連鎖律/MLStudy04
hachiilcane
0
41
機械学習勉強会05 パーセプトロン/MLStudy05
hachiilcane
0
41
機械学習勉強会06 ロジスティック回帰/MLStudy06
hachiilcane
0
56
機械学習勉強会07 ROC曲線/MLStudy07
hachiilcane
0
39
機械学習勉強会08 2次元入力3クラス分類/MLStudy08
hachiilcane
0
66
機械学習勉強会09 2層フィードフォワードニューラルネット/MLStudy09
hachiilcane
0
69
Other Decks in Programming
See All in Programming
ThorVG Viewer In VS Code
nors
0
770
CSC307 Lecture 08
javiergs
PRO
0
670
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
150
Grafana:建立系統全知視角的捷徑
blueswen
0
330
CSC307 Lecture 05
javiergs
PRO
0
500
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
990
Implementation Patterns
denyspoltorak
0
280
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
5
340
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
180
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
420
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
250
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
660
Featured
See All Featured
Being A Developer After 40
akosma
91
590k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
196
71k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
1.9k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
170
GitHub's CSS Performance
jonrohan
1032
470k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
63
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
150
We Are The Robots
honzajavorek
0
160
Automating Front-end Workflow
addyosmani
1371
200k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
130
Transcript
WPFษڧձ ୈ̏ճ ίϨΫγϣϯͷ σʔλόΠϯσΟϯά
ࠓ֮͑ͯ΄͍͜͠ͱ ItemsSourceΛ࣋ͭίϯτϩʔϧ͕ίϨΫ γϣϯΛѻ͑Δ ObservableCollection<T>ɺͱʹ͔ͬ͘ ͱ͚ ίϨΫγϣϯͷཁૉͷݟͤํΛܾΊΔͷ ͕DataTemplete
σʔλόΠϯσΟϯά ͬ͘͟Γ̎ύλʔϯ Ұͭͷʹରͯ͠ ίϨΫγϣϯʹରͯ͠ ࠓճͬͪ͜
͍ͭίϨΫγϣϯͷόΠ ϯσΟϯάΛ͏ͷ͔ ෳϨίʔυͷσʔλΛදࣔ͢Δͱ͖ ΄΅ͯ͢ ࢝Ίσʔλ͕̌ݸͱ͔ɺޙ͔Βσʔ λ͕૿͑Δͱ͔ɺͥΜͥΜOKʂ
ίϨΫγϣϯͱݴ͑Ͳ ͋͘·ͰϓϩύςΟͱϓϩύςΟͰόΠ ϯσΟϯά͢ΔجຊมΘΒͳ͍ όΠϯσΟϯάιʔεଆͷϓϩύςΟͷ ܕ͕ίϨΫγϣϯܕ͚ͬͯͩ
༻Ͱ͖ΔUIίϯτϩʔ ϧݶΒΕ͍ͯΔ ItemsSourceϓϩύςΟɺίϨΫγϣϯ ΛόΠϯσΟϯάͰ͖Δͷʢ΄΅ʣ͜ ͍͚ͭͩ
ItemsSourceϓϩύςΟ Λ࣋ͭίϯτϩʔϧͷྫ ListBox ListView TreeView DataGrid ItemsControl Menu C1.WPF.C1Cha rt.ChartData
ͬͱ؆୯ͳྫ ListBoxʹObservableCollection<string>Λ όΠϯυͤ͞Δ ViewModelΫϥε ObservableCollection<string> AnimalList ViewΫϥε(XAML) ItemsSourceϓϩύςΟ όΠϯσΟϯάιʔε όΠϯσΟϯάλʔήοτ
←͜Μͳͷ͕Ͱ͖Δ DataContext ListBox
Listͷදࣔͷํܾ ·͍ͬͯͳ͍ ίϨΫγϣϯͷதҰͭҰͭͷදࣔͷ ํDataTemplateͰࢦఆ͢Δ
DataTemplateద༻ྫ <ListBox x:Name="SimplyListBox" ItemsSource="{Binding SimplyList}"> <ListBox.ItemTemplate> <DataTemplate> <Border CornerRadius="0" BorderBrush="Black"
BorderThickness="1"> <Grid Margin="3" Width="100"> <TextBlock Text="{Binding}"/> </Grid> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox> public class AnimalsViewModel : ViewModelBase { public ObservableCollection<string> SimplyList { get; set; }
߹͍ݴ༿ ItemsSourceϓϩύςΟͱ DataTemplateɺηοτͰ͓τΫʂ
ίϨΫγϣϯͰ͋ͬͯ ௨ʂ ίϨΫγϣϯͷ߹ɺՃɺมߋɺআΛ۠ ผͯ͠ෳࡶͳ௨ΛߦΘͳ͚ΕͳΒͳ͍ ObservableCollection<T>ͦͷลΛࣗಈͰ ͬͯ͘ΕΔʂ ObservableCollection<T>ͷTมԽ͢ΔͳΒ TINotifyPropertyChanged͕ඞཁ
ίϨΫγϣϯͷόΠϯ σΟϯά·ͱΊΔͱ ViewModelΫϥε ObservableCollection<T>ͷϓϩύςΟ ViewΫϥε(XAML) ItemsSourceϓϩύςΟ όΠϯσΟϯάιʔε όΠϯσΟϯάλʔήοτ DataTemplate ListBoxͱ͔
DataContext
DataTemplateͷͳ͔Ͱ DataContext͕มΘΔ͜ͱʹҙ <ListBox ItemsSource="{Binding SimplyList}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock
Text=“{Binding Species}"/> <TextBlock Text=“{Binding Name}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> public class AnimalsViewModel : ViewModelBase { public ObservableCollection<AnimalViewModel> SimplyList { get; set; } } public class AnimalViewModel : ViewModelBase { public string Species { get; set; } // ←INotifyPropertyChangedΛ࣮͍ͯ͠ͳ͍ͷͰ public string Name { get; set; }ɹ // ·͕ͪͬͨྫ͕ͩΠϝʔδͩͱࢥ͍ͬͯͩ͘͞ } public particle class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new AnimalsViewModel(); } } ViewModelଆ Viewଆ ͜ΕDataContextͷઃఆͷํͱͯ͋͠·Γ ͍͍ྫͰͳ͍͕ɺͬͱ؆୯ͳΓํ
Ԡ༻ྫɿDataGrid ྻͷఆٛͷͳ͔ͰόΠϯυ͢Δ͜ͱ͕ଟ͍ <DataGrid ItemsSource="{Binding ServiceStatusList}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="αʔϏε" Width="160"
DisplayIndex="0" Binding="{Binding Name}"> </DataGridTextColumn> <DataGridTextColumn Header="Քಇঢ়ଶ" Width="80" DisplayIndex="1" Binding="{Binding Status}"> <DataGridTextColumn.ElementStyle> <Style TargetType="{x:Type TextBlock}" > <Setter Property="Margin" Value="2" /> <Setter Property="TextAlignment" Value="Center" /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> </DataGrid.Columns> </DataGrid>
Ԡ༻ྫɿDataGridͱ DataTable ԿͷྻΛԿݸදࣔ͢Δͷ͔ܾ·ͬͯແ ͍ ͋Β͔͡ΊϓϩύςΟΛఆ͓ٛͯ͠ ͘͜ͱ͕Ͱ͖ͳ͍ ͦΜͳͱ͖DataTableΛDataContextʹ όΠϯυͤ͞Δ
DataGridͱDataTableͷ ૬ੑ DB͔ΒऔͬͨΛͦͷ··ͲʔΜʂ DataTable dt = null; string connectionString =
this.serverConnectionInfo.CreateBuilder().ConnectionString; using (SqlConnection con = new SqlConnection(connectionString)) { string query = “select * from aaaTable"; SqlCommand command = new SqlCommand(query, con); SqlDataAdapter a = new SqlDataAdapter(command); DataSet ds = new DataSet(); a.Fill(ds); dt = ds.Tables[0]; } this.LayoutRootGrid.DataContext = dt; <DataGrid ItemsSource="{Binding}"> </DataGrid>
ίπ DataTempleෳ༻ҙͰ͖Δ ίϨΫγϣϯͷܕʹΑ͍͚ͬͯΔ ˠDataTypeϓϩύςΟΛ͏ ಛఆͷϓϩύςΟͷʹΑ͍͚ͬͯΔ ˠDataTemplateSelectorΛ͏ DataTemplateͷதʹDataTriggerͷࢦఆͰ͖Δ
͜ΜͳͷΛߟ͑ͯΈ· ͠ΐ͏