MAR
15
以前公開したiKnow ItemBankPanel for VisualStudioをM-V-VMパターンを使って書き直しています。まだ途中ですが、一応辞書を引いて結果を表示するとこまでは出来たので、M-V-VMパターンの参考までに公開してみます。WPF自体よく分かっていなかった前のコードと比べると見違えるような綺麗さになりましたw
SharpLab.IKnow.ItemBankPanes.zip
添付ビヘイビアでTextBoxにCommandを実装してみた – SharpLab.の添付ビヘイビアは上の検索ツールバーで、添付ビヘイビア試してみた – SharpLab.で紹介した添付ビヘイビアは検索結果を表示しているListBoxで実際に使用しています。
M-V-VMパターンの威力
埋め草までに、ViewのXAMLコードを貼っておきます。このアプリではUIロジックに複雑なところがなかったので、実際、コードビハインドにイベントハンドラを書かずに済みました。ボタンクリックやTextBoxでのEnterキー押下時の処理は、全てCommandを使ってViewModelに移譲しています。
<UserControl x:Class="SharpLab.IKnow.ItemBankPane.ItemBankPaneView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SharpLab.IKnow.ItemBankPane"
>
<UserControl.Resources>
<ResourceDictionary>
<local:ItemBankPaneViewModel x:Key="viewModel" />
</ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Mode="OneTime" Source="{StaticResource viewModel}" />
</UserControl.DataContext>
<DockPanel>
<ToolBar DockPanel.Dock="Top" Height="30" VerticalAlignment="Top">
<TextBox local:TextBoxBehavior.Command="{Binding Mode=OneTime, Path=StartSearchCommand}" HorizontalAlignment="Left" Text="{Binding Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Path=Keyword}" VerticalAlignment="Top" Width="184" />
<Button Command="{Binding Mode=OneTime, Path=StartSearchCommand}" Content="Search"></Button>
</ToolBar>
<ContentControl>
<local:ResultPageView DataContext="{Binding Mode=OneTime, Path=ResultPage}" />
</ContentControl>
</DockPanel>
</UserControl>
<UserControl x:Class="SharpLab.IKnow.ItemBankPane.ResultPageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SharpLab.IKnow.ItemBankPane"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="300"
>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/ResultPageResources.xaml"/>
<ResourceDictionary Source="Resources/VocabularyViewerStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid DockPanel.Dock="Bottom">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="40" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox Name="VocabularyViewer" Grid.Row="0" Grid.ColumnSpan="3" ItemsSource="{Binding Mode=OneWay, NotifyOnTargetUpdated=true, Path=Items}" Style="{StaticResource VocabularyViewerStyle}" />
<Button Grid.Row="1" Grid.Column="0" Margin="6,6,6,6" Command="{Binding Mode=OneTime, Path=NavigatePrevPageCommand}" Content="Prev"/>
<Button Grid.Row="1" Grid.Column="2" Margin="6,6,6,6" Command="{Binding Mode=OneTime, Path=NavigateNextPageCommand}" Content="Next"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Mode=OneWay, Path=PageCounter}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</UserControl>
<UserControl x:Class="SharpLab.IKnow.ItemBankPane.ItemView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SharpLab.IKnow.ItemBankPane"
>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/PlusButton.xaml"/>
<ResourceDictionary Source="Resources/PlayButton.xaml"/>
<ResourceDictionary Source="Resources/SentenceListViewerStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<DockPanel>
<Expander x:Name="Sentences" DockPanel.Dock="Bottom" TabIndex="3" Visibility="{Binding Mode=OneTime, Path=SentencesVisility}" >
<Expander.Header>
<TextBlock Text="Example Sentences:" />
</Expander.Header>
<ItemsControl Name="SentenceListViewer" ItemsSource="{Binding Mode=OneTime, Path=Sentences}" Style="{StaticResource SentenceListViewerStyle}" />
</Expander>
<!--DropDown="{DynamicResource VocabularyViewerItemContextMenu}"-->
<local:DropDownButton DockPanel.Dock="Right" Width="24" Height="24" Style="{StaticResource PlusButtonStyle}" TabIndex="1" Margin="0,0,8,0" />
<DockPanel DockPanel.Dock="Top" LastChildFill="true">
<MediaElement x:Name="mp3Player" LoadedBehavior="Manual" Source="{Binding Mode=OneTime, Path=CueSound}" Width="0" Height="0" />
<Button DockPanel.Dock="Left" Width="18" Height="18" Margin="2,0,4,0" TabIndex="0" Style="{StaticResource PlayButtonStyle}" Command="{Binding Mode=OneTime, Path=PlaySoundCommand}" CommandParameter="{Binding ElementName=mp3Player}" />
<ContentControl Content="{Binding Mode=OneTime, Path=CueText}" IsTabStop="False" />
</DockPanel>
<TextBlock DockPanel.Dock="Top" FontSize="12" Margin="20,0,0,0" Text="{Binding Mode=OneTime, Path=Meaning}" TextWrapping="Wrap" />
</DockPanel>
</UserControl>
<UserControl x:Class="SharpLab.IKnow.ItemBankPane.SentenceView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SharpLab.IKnow.ItemBankPane"
>
<UserControl.Resources>
<local:StringToTextBlockConverter x:Key="StringToTextBlockConverter" />
</UserControl.Resources>
<UserControl.Content>
<Binding Path="Text" Mode="OneTime" Converter="{StaticResource StringToTextBlockConverter}" />
</UserControl.Content>
</UserControl>
Related Entries
Trackbacks : 1
- Trackback URL for this entry
- http://blog.sharplab.net/computer/cprograming/wpf/1866/trackback/
Listed below are links to weblogs that reference this entry
- ピンバック from 今後の予定(2009-04-16版) - SharpLab. 09-04-16 01:06:40 JST
-
[...] iKnow ItemBankPane いまやsmart.fmですが。M-V-VMでの書き直しはM-V-VMパターンでsmart.fmの辞書アプリを書きなおしてみた – SharpLab.という記事以来進んでいません。設計上で詰まっているわけでは [...]
添付ビヘイビアでTextBoxにCommandを実装してみた