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
AIによる高速開発をどう制御するか? ガードレール設置で開発速度と品質を両立させたチームの事例
tonkotsuboy_com
7
2k
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
170
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
680
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
370
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
Basic Architectures
denyspoltorak
0
660
Implementation Patterns
denyspoltorak
0
280
Fragmented Architectures
denyspoltorak
0
150
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
4
320
AgentCoreとHuman in the Loop
har1101
5
220
CSC307 Lecture 01
javiergs
PRO
0
690
Patterns of Patterns
denyspoltorak
0
1.4k
Featured
See All Featured
Scaling GitHub
holman
464
140k
The Cult of Friendly URLs
andyhume
79
6.8k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.8k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.4k
Fireside Chat
paigeccino
41
3.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
34k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.5k
Testing 201, or: Great Expectations
jmmastey
46
8k
How to Talk to Developers About Accessibility
jct
2
130
Balancing Empowerment & Direction
lara
5
880
Bash Introduction
62gerente
615
210k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
170
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ͷࢦఆͰ͖Δ
͜ΜͳͷΛߟ͑ͯΈ· ͠ΐ͏