EventHandler handler = null;
handler = (object sender, EventArgs e) =>
{
objWindow.Closed -= handler;
};
objWindow.Closed += handler;
ラムダ式を用いたイベントハンドラの削除
naudioを用いた音楽再生
MainWindow.xaml
<Window x:Class="WpfApp25.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp25"
mc:Ignorable="d"
Title="MainWindow" Height="142" Width="404">
<Grid>
<TextBox x:Name="txtFilePath" Margin="40,10,46,73"/>
<Button Content="再生" Margin="100,60,96,21" Click="Button_Click"/>
</Grid>
</Window>
ToastNotificationsを用いた通知表示
なお、ToastNotificationsには、MessageOptions内にNotificationClickActionというクリック通知用アクションが用意されているが、
うまく動作しなかったため、代替処理を行っている。
Windowの子要素を再帰検索する
MainWindow.xaml
<Window x:Class="WpfApp24.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp24"
mc:Ignorable="d"
Title="MainWindow" Height="242" Width="409">
<Grid>
<TextBox HorizontalAlignment="Left" Height="30" Margin="65,10,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="90"/>
<Grid HorizontalAlignment="Left" Height="125" Margin="10,60,0,0" VerticalAlignment="Top" Width="365" Background="#FF66EC49">
<Grid HorizontalAlignment="Left" Height="60" Margin="10,55,0,0" VerticalAlignment="Top" Width="150" Background="#FFEA7878">
<TextBox HorizontalAlignment="Left" Height="35" Margin="10,10,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="105"/>
</Grid>
<TextBox HorizontalAlignment="Left" Height="35" Margin="55,15,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="105"/>
<Grid HorizontalAlignment="Left" Height="60" Margin="190,55,0,0" VerticalAlignment="Top" Width="150" Background="#FF7670F7">
<TextBox HorizontalAlignment="Left" Height="35" Margin="10,10,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="105"/>
</Grid>
</Grid>
</Grid>
</Window>
Windowの最大化を抑止する
MainView.xaml
<Metro:MetroWindow
x:Class="WpfApp21.View.MainView"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp21.View"
mc:Ignorable="d"
Title="MainView" Height="450" Width="800"
x:Name="viewMainView"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
xmlns:vm="clr-namespace:WpfApp21.ViewModel"
xmlns:Metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
ShowMaxRestoreButton="False"
>
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
<i:Interaction.Triggers>
<i:EventTrigger EventName ="StateChanged">
<l:LivetCallMethodAction MethodTarget="{Binding}" MethodName="OnStateChanged" MethodParameter="{Binding ElementName=viewMainView}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
</Grid>
</Metro:MetroWindow>
TextBoxを複数行入力できるようにする
AcceptsReturn="True" を付ける。
MainWindow.xaml
<Window x:Class="WpfApp17.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp17"
mc:Ignorable="d"
Title="MainWindow" Height="250" Width="400"
WindowStartupLocation="CenterScreen">
<Grid>
<TextBox Grid.Row="0" AcceptsReturn="True" Text="{Binding Text}" />
</Grid>
</Window>
ListViewのスクロールをPixel単位にする
VirtualizingPanel.ScrollUnit="Pixel" を設定する。
MainWindow.xaml
<Window x:Class="WpfApp19.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp19"
mc:Ignorable="d"
Title="MainWindow" Height="250" Width="400">
<Grid>
<ListView x:Name="lstView" Margin="30" ItemsSource="{Binding arrayString}" VirtualizingPanel.ScrollUnit="Pixel">
<ListView.ItemTemplate>
<DataTemplate>
<Button Height="50" Width="100" Content="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
別スレッドで編集されたコレクションをBindする
別スレッドでアイテム追加等を行ったコレクションをBindすると、
「System.NotSupportedException: 'この型の CollectionView は、Dispatcher スレッドとは異なるスレッドからその SourceCollection への変更をサポートしません。’」
というエラーが出る。
CollectionSynchronization にて、参加指定する必要がある。
ウィンドウのサイズを内容に合わせる
MainWindow.xaml
<Window x:Class="WpfApp17.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp17"
mc:Ignorable="d"
Title="MainWindow" Height="250" Width="400"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" AcceptsReturn="True" Text="{Binding Text}" />
<Button Grid.Row="1" Content="確認" Click="Button_Click" />
</Grid>
</Window>