• 検索結果がありません。

wpf #wpf

N/A
N/A
Protected

Academic year: 2021

シェア "wpf #wpf"

Copied!
111
0
0

読み込み中.... (全文を見る)

全文

(1)
(2)

1

1: wpf

2

2

2

Examples

2

Hello World

2

2: ""

7

7

Examples

7

7

11

3: System.Windows.Controls.WebBrowser

13

13

13

Examples

13

BusyIndicatorWeb

13

4: WPF

14

14

Examples

14

14

15

ComboBox

17

21

DoubleAnimation

22

5: WPF

25

25

25

26

UpdateSourceTrigger

26

Examples

26

26

(3)

DataContext

27

INotifyPropertyChanged

27

28

29

29

6: WPF

31

31

Examples

31

31

33

35

36

7: WPFMVVM

39

39

Examples

39

WPFCMVVM

39

42

43

45

MVVM

46

8: WPF

49

Examples

49

DispatcherObject

49

49

49

49

DependencyObject

49

49

49

49

9: WPF

50

50

(4)

50

50

50

Examples

50

51

51

51

10: WPF

53

53

Examples

53

53

11: WPF

55

Examples

55

55

55

56

57

12: WPF

60

60

Examples

60

VBXAML

60

VB

60

XAML for C

61

61

13:

63

63

63

Examples

63

63

XAML

68

14:

70

(5)

Examples

70

70

/

70

70

15:

72

72

72

Examples

72

72

XAML

73

16: UI

74

Examples

74

UI

74

17:

76

Examples

76

Windows 8Windows 10

76

.NET Framework 4.6.2WPF

76

.NET Framework 4.6.1WPF

76

77

Windows 10

78

Windows 10

78

18: UserControl

80

80

Examples

80

ComboBox

80

19:

83

83

83

Examples

83

83

83

(6)

DataTrigger

84

20:

85

85

85

IValueConverterIMultiValueConverter

85

85

Examples

85

BooleanToVisibilityConverter [IValueConverter]

85

86

[IValueConverter]

87

88

[IMultiValueConverter]

88

88

ConverterParameter

89

90

[IValueConverter]

90

MarkupExtension

91

IMultiValueConverter

92

21:

94

94

94

Examples

94

IValueConverter

94

XAML

95

22:

96

96

96

96

Examples

97

97

97

(7)

97

97

98

98

98

99

100

100

100

100

23:

102

102

102

Examples

102

- Hello World

102

103

(8)

You can share this PDF with anyone you feel could benefit from it, downloaded the latest version

from:

wpf

It is an unofficial and free wpf ebook created for educational purposes. All the content is extracted

from

Stack Overflow Documentation

, which is written by many hardworking individuals at Stack

Overflow. It is neither affiliated with Stack Overflow nor official wpf.

The content is released under Creative Commons BY-SA, and the list of contributors to each

chapter are provided in the credits section at the end of this book. Images may be copyright of

their respective owners unless otherwise specified. All trademarks and registered trademarks are

the property of their respective company owners.

Use the content presented in this book at your own risk; it is not guaranteed to be correct nor

accurate, please send your feedback and corrections to

[email protected]

(9)

1: wpfをいめる

WPFWindows Presentation Foundation

は、の

Windows

デスクトップアプリケーションで

Microsoft

がするプレゼンテーションテクノロジです。

WPF

は

UWPUniversal Windows Platform

とされるべ

きではありませんが、のにはがあります。

WPF

は、マルチメディア、アニメーション、データバインディングにをいてデータアプリケーシ

ョンをします。インターフェイスは、

XML

のである

XAMLeXtensible Application Markup

Language

とばれるをしてされます。

XAML

は

WPF

プログラマーがビジュアルデザインとインタ

ーフェイスロジックのをするのにちます。

の

Windows

フォームとはなり、

WPF

はボックスモデルをしてインターフェイスのすべてのをレイ

アウトします。は、さ、、およびをち、にしてににされます。

WPF

は

Windows Presentation Foundation

ので、コードネーム

Avalon

でもられています。これはグ

ラフィカルなフレームワークで、

Microsoft .NET Framework

のです。

WPF

は

Windows Vista

、

7,8、および10にプリインストールされており、

Windows XP

および

Server 2003

にインストール

できます。

バージョン

バージョン4.6.1

201512

Examples

Hello World

アプリケーション

Visual Studio

でしい

WPF

プロジェクトをしてするには

ファイル→→プロジェクトをクリックします。

1.

(10)

テンプレート→

Visual C

→Windows→WPFアプリケーションをクリックしてテンプレート

をし、 OK

をします。

(11)

ソリューションエクスプローラで

MainWindow.xaml

ファイルをきます

ソリューションエ

クスプローラウィンドウがされないは、 →ソリューションエクスプローラをクリックして

きます

。

(12)

XAML

セクションデフォルトではデザインセクションのにこのコードをします

4.

<TextBlock>Hello world!</TextBlock>

の

Grid

タグ

コードはのようになります。

<Window x:Class="WpfApplication1.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"

(13)

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1"

mc:Ignorable="d"

Title="MainWindow" Height="350" Width="525"> <Grid> <TextBlock>Hello world!</TextBlock> </Grid> </Window> F5

キーをすか、 デバッグ→デバッグをクリックして、アプリケーションをします。それは

のようになります

5.

オンラインでwpfをいめるをむ

https://riptutorial.com/ja/wpf/topic/820/wpf

をいめる

(14)

2: "の"のデザイン

き

コントロールをレイアウトするとき、マージンやパディングののをハードコーディングして、の

レイアウトにフィットさせるのはです。しかし、これらのをハードコーディングすることにより

、メンテナンスははるかにになります。レイアウトがされた、なとなされるかもしれませんが、

くのがこれらのをするがあります。

このは、レイアウトをなるでえることによってレイアウトのコストをします。

Examples

とそののデモンストレーション

たとえば、のように3つのセクションがあるスクリーンをしてみてください。

いボックスには4,4,0,0のマージンがえられます。のボックスには4,4,4,0のマージンがえられます

。のボックスマージンは4,4,4,4です。

XAML

はのとおりですレイアウトをするためにグリッドを

していますが、このデザインのはレイアウトのになくされます。

<UserControl x:Class="WpfApplication5.UserControl1HardCoded"

(15)

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="3*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="2*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions>

<Border Grid.Column="0" Grid.Row="0" Margin="4,4,0,0" Background="DodgerBlue" BorderBrush="DarkBlue" BorderThickness="5"/>

<Border Grid.Column="1" Grid.Row="0" Margin="4,4,4,0" Background="Green" BorderBrush="DarkGreen" BorderThickness="5"/>

<Border Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Margin="4,4,4,4" Background="MediumPurple" BorderBrush="Purple" BorderThickness="5"/>

</Grid> </UserControl>

いボックスのにのボックスをくように、レイアウトをしたいとえてみましょう。シンプルでなけ

ればならないのですかそれをいて、たちはそのをかすときにをつけるがあります。いボックスの

を0,4,4,0にすることができます。を4,4,4,0に、を4,4,0,0にすることができます。ここに

XAML

が

あります

<UserControl x:Class="WpfApplication5.UserControl2HardCoded" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="3*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="2*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions>

<Border Grid.Column="1" Grid.Row="0" Margin="4,4,4,0" Background="DodgerBlue" BorderBrush="DarkBlue" BorderThickness="5"/>

<Border Grid.Column="0" Grid.Row="0" Margin="4,4,0,0" Background="Green" BorderBrush="DarkGreen" BorderThickness="5"/>

<Border Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Margin="4,4,4,4" Background="MediumPurple" BorderBrush="Purple" BorderThickness="5"/>

</Grid> </UserControl>

(16)

<UserControl x:Class="WpfApplication5.UserControl3HardCoded" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="3*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="2*"/> </Grid.RowDefinitions>

<Border Grid.Column="1" Grid.Row="1" Margin="4,0,4,4" Background="DodgerBlue" BorderBrush="DarkBlue" BorderThickness="5"/>

<Border Grid.Column="0" Grid.Row="1" Margin="4,0,0,4" Background="Green" BorderBrush="DarkGreen" BorderThickness="5"/>

<Border Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Margin="4,4,4,4" Background="MediumPurple" BorderBrush="Purple" BorderThickness="5"/>

</Grid> </UserControl>

たちがをかすことができれば、これらのをまったくするがないので、うれしいことではないでし

ょうか。これは、なるでをえるだけでできます。すべてのを1つのコントロールにりてるのではな

く、のがボックスにりてられているとしますのはあまりスケーリングされません - はボックスの

とそのののにするがあります 。

(17)

したがって、のボックスには2,2,2,2のマージンがあります。のボックスのは2,2,2,2です。のボッ

クスのは2,2,2,2です。そしてそれらがされているコンテナには2,2,2,2のパディングマージンでは

ないがえられます。ここに

XAML

があります

<UserControl x:Class="WpfApplication5.UserControl1HalfTheWhitespace" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Padding="2,2,2,2"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="3*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="2*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions>

<Border Grid.Column="0" Grid.Row="0" Margin="2,2,2,2" Background="DodgerBlue" BorderBrush="DarkBlue" BorderThickness="5"/>

<Border Grid.Column="1" Grid.Row="0" Margin="2,2,2,2" Background="Green" BorderBrush="DarkGreen" BorderThickness="5"/>

<Border Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Margin="2,2,2,2" Background="MediumPurple" BorderBrush="Purple" BorderThickness="5"/>

</Grid> </UserControl>

さあ、とじでボックスをかしてみましょう...いボックスのにのボックスをれましょう。

OK

、しま

した。パディングやマージンをするはありませんでした。ここに

XAML

があります

<UserControl x:Class="WpfApplication5.UserControl2HalfTheWhitespace" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Padding="2,2,2,2"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="3*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="2*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions>

<Border Grid.Column="1" Grid.Row="0" Margin="2,2,2,2" Background="DodgerBlue" BorderBrush="DarkBlue" BorderThickness="5"/>

<Border Grid.Column="0" Grid.Row="0" Margin="2,2,2,2" Background="Green" BorderBrush="DarkGreen" BorderThickness="5"/>

<Border Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Margin="2,2,2,2" Background="MediumPurple" BorderBrush="Purple" BorderThickness="5"/>

(18)

</Grid> </UserControl>

さあ、のボックスをにきましょう。

OK

、しました。パディングやマージンをするはありません

でした。ここに

XAML

があります

<UserControl x:Class="WpfApplication5.UserControl3HalfTheWhitespace" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Padding="2,2,2,2"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="3*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="2*"/> </Grid.RowDefinitions>

<Border Grid.Column="1" Grid.Row="1" Margin="2,2,2,2" Background="DodgerBlue" BorderBrush="DarkBlue" BorderThickness="5"/>

<Border Grid.Column="0" Grid.Row="1" Margin="2,2,2,2" Background="Green" BorderBrush="DarkGreen" BorderThickness="5"/>

<Border Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Margin="2,2,2,2" Background="MediumPurple" BorderBrush="Purple" BorderThickness="5"/>

</Grid> </UserControl>

のコードでこれをう

にしたことをするために、々のものには " の

"

のマージンがまれていて、されているコンテナには

"スペース"のパディングがです。これらのスタイルをアプリケーションのリソースにすると、々

のアイテムについてするはありません。 「

HalfTheWhiteSpace

」をするはのとおりです。

<system:Double x:Key="DefaultMarginSize">2</system:Double>

<Thickness x:Key="HalfTheWhiteSpace" Left="{StaticResource DefaultMarginSize}" Top="{StaticResource DefaultMarginSize}" Right="{StaticResource DefaultMarginSize}" Bottom="{StaticResource DefaultMarginSize}"/>

に、のコントロールスタイルをベースにするベーススタイルをすることができますこれには、デ

フォルトの

FontFamily

、FontSizeなどもまれます

<Style x:Key="BaseStyle" TargetType="{x:Type Control}">

<Setter Property="Margin" Value="{StaticResource HalfTheWhiteSpace}"/> </Style>

(19)

<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}"/>

DatePickers

、Labelsなどコンテナにされているのあるものには、このようなことができます。こ

のような

TextBlock

のスタイリングにしてください。コントロールはくのコントロールによってに

されます。

TextBlock

からしたのコントロールをすることをおめします。デフォルトマージンを

するように

TextBlock

をスタイルすることができます 。 XAMLでに

TextBlock

をするはず

TextBlock

をしてください 。

のアプローチをして、のコンテナたとえば、

ScrollViewer

、Borderなどにパディングをすることが

できます。

これをませたら、 ほとんどのコントロールはマージンとパディングをとせず、にこのデザインか

らしたいにをするだけでみます。

オンラインで"の"のデザインをむ

https://riptutorial.com/ja/wpf/topic/9407/-

の

-

のデザイン

(20)

3: System.Windows.Controls.WebBrowser

き

これにより、

Web

ブラウザを

WPF

アプリケーションにみむことができます。

ドキュメンテーションからではなく、もから

InternetExplorer

をインストールしているのではなく

、

InternetExplorer7

のようにすることがです

https// weblog

を

してください

。

west-wind.com/posts/2011/may/21/web-browser-control-specifying-the-ie-version

。

これは、コントロールにプロパティをすることですることはできません。

HTML

メタタグをする

か、レジストリをして、されているページをするがあります。 のアプローチのはのリンクにあり

ます。

たとえば、このなデザインのによって、「スクリプトエラー」/「このページのスクリプトにエラ

ーがしました」というメッセージがされるがあります。このエラーをべると、のをしてしいをす

るのではなく、エラーをすることがであるとえるかもしれません。

Examples

BusyIndicator

の

Web

ブラウザーの

WebBrowser

コントロールは、

XAML

にではなく、のもののにされることにしてください。たとえ

ば、ビジーであるとマークされている

BusyIndicator

のにすると、そのコントロールのにがレンダ

リングされます。は、

WebBrowser

のを

BusyIndicator

がしているにバインドし、コンバータをし

て

Boolean

をし、

Visibility

にすることです。えば

<telerik:RadBusyIndicator IsBusy="{Binding IsBusy}">

<WebBrowser Visibility="{Binding IsBusy, Converter={StaticResource InvertBooleanToVisibilityConverter}}"/>

</telerik:RadBusyIndicator>

オンラインでSystem.Windows.Controls.WebBrowserをむ

(21)

4: WPFスタイルの

き

スタイルをすると、

WPF

コントロールのをにできます。なスタイリングのいくつかのと、リソー

スとアニメーションのです。

Examples

ボタンのスタイル

スタイルをするもなは、のスタイルをコピーしてすることです。

2

つのボタンでなウィンドウをします。

<Window x:Class="WPF_Style_Example.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" mc:Ignorable="d" ResizeMode="NoResize" Title="MainWindow" Height="150" Width="200"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions>

<Button Margin="5" Content="Button 1"/>

<Button Margin="5" Grid.Row="1" Content="Button 2"/> </Grid>

Visual Studio

では、エディタののボタンをクリックし、[テンプレートの

]

メニューの[コピーの

...]

をするとコピーできます。

「アプリケーション」でします。

のは、ボタンをするためのされたテンプレートをしています。

<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">

<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>

<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/> <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static

SystemColors.ControlTextBrushKey}}"/>

<Setter Property="BorderThickness" Value="1"/>

<Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Padding" Value="1"/>

(22)

<Setter.Value>

<ControlTemplate TargetType="{x:Type Button}"> <Grid>

<Ellipse x:Name="ellipse" StrokeThickness="{TemplateBinding

BorderThickness}" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}" SnapsToDevicePixels="true"/>

<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding

SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Grid>

<ControlTemplate.Triggers>

<Trigger Property="IsDefaulted" Value="true"> <Setter Property="Stroke" TargetName="ellipse" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger>

<Trigger Property="IsMouseOver" Value="true"> <Setter Property="Fill" TargetName="ellipse" Value="{StaticResource Button.MouseOver.Background}"/>

<Setter Property="Stroke" TargetName="ellipse" Value="{StaticResource Button.MouseOver.Border}"/>

</Trigger>

<Trigger Property="IsPressed" Value="true"> <Setter Property="Fill" TargetName="ellipse" Value="{StaticResource Button.Pressed.Background}"/>

<Setter Property="Stroke" TargetName="ellipse" Value="{StaticResource Button.Pressed.Border}"/>

</Trigger>

<Trigger Property="IsEnabled" Value="false"> <Setter Property="Fill" TargetName="ellipse" Value="{StaticResource Button.Disabled.Background}"/>

<Setter Property="Stroke" TargetName="ellipse" Value="{StaticResource Button.Disabled.Border}"/>

<Setter Property="TextElement.Foreground"

TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

すべてのボタンにされるスタイル

のでは、スタイルの

xKey

をすると、

Application

スコープのすべてのボタンにスタイルがされます

(23)

。

<Style TargetType="{x:Type Button}">

<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>

<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/> <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static

SystemColors.ControlTextBrushKey}}"/>

<Setter Property="BorderThickness" Value="1"/>

<Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Padding" Value="1"/>

<Setter Property="Template"> <Setter.Value>

<ControlTemplate TargetType="{x:Type Button}"> <Grid>

<Ellipse x:Name="ellipse" StrokeThickness="{TemplateBinding

BorderThickness}" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}" SnapsToDevicePixels="true"/>

<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding

SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Grid>

<ControlTemplate.Triggers>

<Trigger Property="IsDefaulted" Value="true"> <Setter Property="Stroke" TargetName="ellipse" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger>

<Trigger Property="IsMouseOver" Value="true"> <Setter Property="Fill" TargetName="ellipse" Value="{StaticResource Button.MouseOver.Background}"/>

<Setter Property="Stroke" TargetName="ellipse" Value="{StaticResource Button.MouseOver.Border}"/>

</Trigger>

<Trigger Property="IsPressed" Value="true"> <Setter Property="Fill" TargetName="ellipse" Value="{StaticResource Button.Pressed.Background}"/>

<Setter Property="Stroke" TargetName="ellipse" Value="{StaticResource Button.Pressed.Border}"/>

</Trigger>

<Trigger Property="IsEnabled" Value="false"> <Setter Property="Fill" TargetName="ellipse" Value="{StaticResource Button.Disabled.Background}"/>

<Setter Property="Stroke" TargetName="ellipse" Value="{StaticResource Button.Disabled.Border}"/>

<Setter Property="TextElement.Foreground"

TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

々のボタンにスタイルをするはなくなりました。

<Window x:Class="WPF_Style_Example.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

(24)

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" mc:Ignorable="d" ResizeMode="NoResize" Title="MainWindow" Height="150" Width="200"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions>

<Button Margin="5" Content="Button 1"/>

<Button Margin="5" Grid.Row="1" Content="Button 2"/> </Grid>

どちらのボタンもスタイリングされています。

ComboBox

のスタイル

の

ComboBox es

でまる

<Window x:Class="WPF_Style_Example.ComboBoxWindow" 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" mc:Ignorable="d" ResizeMode="NoResize" Title="ComboBoxWindow" Height="100" Width="150"> <StackPanel>

<ComboBox Margin="5" SelectedIndex="0"> <ComboBoxItem Content="Item A"/> <ComboBoxItem Content="Item B"/> <ComboBoxItem Content="Item C"/> </ComboBox>

<ComboBox IsEditable="True" Margin="5" SelectedIndex="0"> <ComboBoxItem Content="Item 1"/>

<ComboBoxItem Content="Item 2"/> <ComboBoxItem Content="Item 3"/> </ComboBox>

(25)

デザイナーのの

ComboBox

クリックし、「テンプレートの

- >

コピーの」をします。アプリケーショ

ンスコープでスタイルをします。

されるスタイルは3つあります。

ComboBoxToggleButton ComboBoxEditableTextBox ComboBoxStyle1

そして2つのテンプレート

ComboBoxTemplate ComboBoxEditableTemplate ComboBoxToggleButton

スタイルの

<SolidColorBrush x:Key="ComboBox.Static.Border" Color="#FFACACAC"/>

<SolidColorBrush x:Key="ComboBox.Static.Editable.Background" Color="#FFFFFFFF"/> <SolidColorBrush x:Key="ComboBox.Static.Editable.Border" Color="#FFABADB3"/>

<SolidColorBrush x:Key="ComboBox.Static.Editable.Button.Background" Color="Transparent"/> <SolidColorBrush x:Key="ComboBox.Static.Editable.Button.Border" Color="Transparent"/> <SolidColorBrush x:Key="ComboBox.MouseOver.Glyph" Color="#FF000000"/>

<LinearGradientBrush x:Key="ComboBox.MouseOver.Background" EndPoint="0,1" StartPoint="0,0">

<GradientStop Color="Orange" Offset="0.0"/> <GradientStop Color="OrangeRed" Offset="1.0"/> </LinearGradientBrush>

<SolidColorBrush x:Key="ComboBox.MouseOver.Border" Color="Red"/>

<SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Background" Color="#FFFFFFFF"/> <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Border" Color="#FF7EB4EA"/>

<LinearGradientBrush x:Key="ComboBox.MouseOver.Editable.Button.Background" EndPoint="0,1" StartPoint="0,0">

<GradientStop Color="#FFEBF4FC" Offset="0.0"/> <GradientStop Color="#FFDCECFC" Offset="1.0"/> </LinearGradientBrush>

<SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Button.Border" Color="#FF7EB4EA"/> <SolidColorBrush x:Key="ComboBox.Pressed.Glyph" Color="#FF000000"/>

<LinearGradientBrush x:Key="ComboBox.Pressed.Background" EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="OrangeRed" Offset="0.0"/>

<GradientStop Color="Red" Offset="1.0"/> </LinearGradientBrush>

<SolidColorBrush x:Key="ComboBox.Pressed.Border" Color="DarkRed"/>

<SolidColorBrush x:Key="ComboBox.Pressed.Editable.Background" Color="#FFFFFFFF"/> <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Border" Color="#FF569DE5"/>

<LinearGradientBrush x:Key="ComboBox.Pressed.Editable.Button.Background" EndPoint="0,1" StartPoint="0,0">

<GradientStop Color="#FFDAEBFC" Offset="0.0"/> <GradientStop Color="#FFC4E0FC" Offset="1.0"/> </LinearGradientBrush>

<SolidColorBrush x:Key="ComboBox.Pressed.Editable.Button.Border" Color="#FF569DE5"/> <SolidColorBrush x:Key="ComboBox.Disabled.Glyph" Color="#FFBFBFBF"/>

<SolidColorBrush x:Key="ComboBox.Disabled.Background" Color="#FFF0F0F0"/> <SolidColorBrush x:Key="ComboBox.Disabled.Border" Color="#FFD9D9D9"/>

<SolidColorBrush x:Key="ComboBox.Disabled.Editable.Background" Color="#FFFFFFFF"/> <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Border" Color="#FFBFBFBF"/> <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Button.Background"

(26)

<SolidColorBrush x:Key="ComboBox.Disabled.Editable.Button.Border" Color="Transparent"/> <SolidColorBrush x:Key="ComboBox.Static.Glyph" Color="#FF606060"/>

<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}"> <Setter Property="OverridesDefaultStyle" Value="true"/>

<Setter Property="IsTabStop" Value="false"/> <Setter Property="Focusable" Value="false"/> <Setter Property="ClickMode" Value="Press"/> <Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type ToggleButton}"> <Border x:Name="templateRoot" CornerRadius="10"

BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource ComboBox.Static.Background}"

SnapsToDevicePixels="true">

<Border x:Name="splitBorder" BorderBrush="Transparent"

BorderThickness="1" HorizontalAlignment="Right" Margin="0" SnapsToDevicePixels="true" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">

<Path x:Name="arrow" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z" Fill="{StaticResource

ComboBox.Static.Glyph}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/> </Border>

</Border>

<ControlTemplate.Triggers> <MultiDataTrigger>

<MultiDataTrigger.Conditions>

<Condition Binding="{Binding IsEditable,

RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/> <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="false"/>

<Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Self}}" Value="false"/>

<Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="true"/>

</MultiDataTrigger.Conditions>

<Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Static.Editable.Background}"/>

<Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Static.Editable.Border}"/>

<Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.Static.Editable.Button.Background}"/>

<Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.Static.Editable.Button.Border}"/>

</MultiDataTrigger>

<Trigger Property="IsMouseOver" Value="true">

<Setter Property="BorderThickness" TargetName="templateRoot" Value="2"/>

</Trigger>

<MultiDataTrigger>

<MultiDataTrigger.Conditions>

<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>

<Condition Binding="{Binding IsEditable,

RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="false"/> </MultiDataTrigger.Conditions>

<Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Background}"/>

<Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Border}"/>

</MultiDataTrigger> <MultiDataTrigger>

(27)

<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>

<Condition Binding="{Binding IsEditable,

RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/> </MultiDataTrigger.Conditions>

<Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Editable.Background}"/>

<Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Editable.Border}"/>

<Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.MouseOver.Editable.Button.Background}"/>

<Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.MouseOver.Editable.Button.Border}"/>

</MultiDataTrigger>

<Trigger Property="IsPressed" Value="true">

<Setter Property="Fill" TargetName="arrow" Value="{StaticResource ComboBox.Pressed.Glyph}"/>

</Trigger>

<MultiDataTrigger>

<MultiDataTrigger.Conditions>

<Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Self}}" Value="true"/>

<Condition Binding="{Binding IsEditable,

RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="false"/> </MultiDataTrigger.Conditions>

<Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Background}"/>

<Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Border}"/>

</MultiDataTrigger> <MultiDataTrigger>

<MultiDataTrigger.Conditions>

<Condition Binding="{Binding IsPressed, RelativeSource={RelativeSource Self}}" Value="true"/>

<Condition Binding="{Binding IsEditable,

RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/> </MultiDataTrigger.Conditions>

<Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Editable.Background}"/>

<Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Pressed.Editable.Border}"/>

<Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.Pressed.Editable.Button.Background}"/>

<Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.Pressed.Editable.Button.Border}"/>

</MultiDataTrigger>

<Trigger Property="IsEnabled" Value="false">

<Setter Property="Fill" TargetName="arrow" Value="{StaticResource ComboBox.Disabled.Glyph}"/>

</Trigger>

<MultiDataTrigger>

<MultiDataTrigger.Conditions>

<Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>

<Condition Binding="{Binding IsEditable,

RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="false"/> </MultiDataTrigger.Conditions>

<Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Background}"/>

<Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Border}"/>

(28)

</MultiDataTrigger> <MultiDataTrigger>

<MultiDataTrigger.Conditions>

<Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>

<Condition Binding="{Binding IsEditable,

RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/> </MultiDataTrigger.Conditions>

<Setter Property="Background" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Editable.Background}"/>

<Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.Disabled.Editable.Border}"/>

<Setter Property="Background" TargetName="splitBorder" Value="{StaticResource ComboBox.Disabled.Editable.Button.Background}"/>

<Setter Property="BorderBrush" TargetName="splitBorder" Value="{StaticResource ComboBox.Disabled.Editable.Button.Border}"/> </MultiDataTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

これにより、マウスのにオレンジがされ、されるとになるみをびた

ComboBox

がされます。

これにより、なコンボボックスはそのにわらないことにしてください。するがあり、

ComboBoxEditableTextBox

スタイルや

ComboBoxEditableTemplate

。

リソースディクショナリの

App.xaml

でくのスタイルをすると、すぐにになり、々のリソースディクショナリにすることがで

きます。

(29)

をするには、そのを

App.xaml

とマージするがあります。したがって、

App.xaml

では、リソースデ

ィクショナリがされた、のようになります。

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WPF_Style_Example.App" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ResourceDictionaries/Dictionary1.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>

Dictionary1.xaml

でしいスタイルをできるようになり、

App.xaml

にあるかのようにできます。プロ

ジェクトをビルドしたは、スタイルをコピーしてしいリソースディクショナリにするときに

Visual Studio

にオプションがされます。

ボタンスタイル

DoubleAnimation

の

Window

がされました

<Window x:Class="WPF_Style_Example.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" mc:Ignorable="d" ResizeMode="NoResize" Title="MainWindow" Height="150" Width="250"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions>

<Button Margin="5" Content="Button 1" Width="200"/>

<Button Margin="5" Grid.Row="1" Content="Button 2" Width="200"/> </Grid>

ボタンにされたスタイル

App.xaml

では、マウスがコントロールにると200から100に、アニメーシ

ョンがると100から200にアニメーションされます。

<Style TargetType="{x:Type Button}">

<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>

<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/> <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static

SystemColors.ControlTextBrushKey}}"/>

<Setter Property="BorderThickness" Value="1"/>

<Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/>

(30)

<Setter Property="Padding" Value="1"/> <Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type Button}"> <Grid Background="White">

<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">

<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding

SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> </Grid> <ControlTemplate.Triggers> <EventTrigger RoutedEvent="MouseEnter"> <BeginStoryboard> <Storyboard>

<DoubleAnimation To="100" From="200"

Storyboard.TargetProperty="Width" Storyboard.TargetName="border" Duration="0:0:0.25"/> </Storyboard> </BeginStoryboard> </EventTrigger> <EventTrigger RoutedEvent="MouseLeave"> <BeginStoryboard> <Storyboard>

<DoubleAnimation To="200" From="100"

Storyboard.TargetProperty="Width" Storyboard.TargetName="border" Duration="0:0:0.25"/> </Storyboard>

</BeginStoryboard> </EventTrigger>

<Trigger Property="IsDefaulted" Value="true">

<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger>

<Trigger Property="IsMouseOver" Value="true">

<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>

<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>

</Trigger>

<Trigger Property="IsPressed" Value="true">

<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>

<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>

</Trigger>

<Trigger Property="IsEnabled" Value="false">

<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>

<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>

<Setter Property="TextElement.Foreground"

TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

(31)
(32)

5: WPFデータバインディングの

{Binding PropertyName}

は

{Binding Path = PropertyName}

にします 。

•

{

バインディングパス=

SomeProperty.SomeOtherProperty.YetAnotherProperty}

•

{

バインディングパス=

SomeListProperty [1]}

•

パラメーター

パラメータ

パス

バインドするパスをします。されていないは、

DataContext

にバイン

ドされます。

UpdateSourceTrigger

バインドのがされるをします。デフォルトは

LostFocus

です。もよく

されるは

PropertyChanged

です。

モード

OneWay

または

TwoWay

。バインディングによってされていないは、バイ

ンディングターゲットが

TwoWay

ことをしないり、デフォルトで

OneWay

になり

TwoWay

。

TwoWay

をしてみりプロパティにバインドするときに

エラーがします。たとえば、みりプロパティを

TextBox.Text

にバイン

ドするときに

OneWay

にするがあります。

ソース

の

DataContext

のわりに

StaticResource

をバインディングソースとし

てできるようにします。

RelativeSource

の

DataContext

ではなく、バインディングソースとしての

XAML

をで

きます。

ElementName

の

DataContext

のわりにき

XAML

をバインディングソースとしてでき

るようにします。

FallbackValue

バインディングがした、このはバインディングターゲットにされま

す。

TargetNullValue

バインディング・ソースが

null

、このはバインディング・ターゲットに

されます。

コンバータ

バインディングのをするためにするコンバータ

StaticResource

をしま

す。たとえば、ブールを

Visibility

アイテムにします。

ConverterParameter

コンバータにされるオプションのパラメータをします。このはでな

ければならず、バインドすることはできません。

StringFormat

バインドされたをするときにするをします。

(33)

パラメータ

ディレイ

WPF 4.5+

バインディングが

ViewModel

の

BindingSource

をするを

milliseconds

でし

milliseconds

。これを

Mode=TwoWay

にするには、

Mode=TwoWay

および

UpdateSourceTrigger=PropertyChanged

をするがありま

す。

UpdateSourceTrigger

では、

WPF

はコントロールがフォーカスをったときにバインディングソースをします。ただし、

フォーカスをできるコントロールが1つしかないでよくられるようなもの、アップデートをさせる

には

UpdateSourceTrigger=PropertyChanged

をするがあります。

すべてのキーストロークでバインディングソースをするのはコストがかかり、ライブデータのが

ましくないをき、くのバインディングで

PropertyChanged

をトリガーとしてしたいがあります。

LostFocus

をするとなことにがあります

Enter

をして

IsDefault

マークされたボタンをしてフォームを

しても、バインディングをバックアップするプロパティはされず、をににします。いにも、

いく

つかのがし

ます。

また、

UWP

とはなり、

WPF4.5

にはバインディングに

Delay

プロパティがあり、の

TextBox

のように

、ローカルのみまたはなマイナーインテリジェンスをつバインディングにはであることにしてく

ださい。

Examples

ブールをにする

このでは、チェックボックスが

IValueConverter

をしてチェックされていない、いボックスがにな

っています。

のでされる

BooleanToVisibilityConverter

は、

System.Windows.Controls

にあるみみのコンバータで

す。

XAML

<Window x:Class="StackOverflowDataBindingExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Height="350" Width="525"> <Window.Resources> <BooleanToVisibilityConverter x:Key="VisibleIfTrueConverter" /> </Window.Resources> <StackPanel> <CheckBox x:Name="MyCheckBox" IsChecked="True" />

<Border Background="Red" Width="20" Height="20"

(34)

Converter={StaticResource VisibleIfTrueConverter}}" /> </StackPanel> </Window>

DataContext

の

WPF

でバインディングをするには、

DataContext

をするがあります。

DataContext

は、デフォル

トでデータをするをバインディングにします。

<Window x:Class="StackOverflowDataBindingExample.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:StackOverflowDataBindingExample" xmlns:vm="clr-namespace:StackOverflowDataBindingExample.ViewModels" mc:Ignorable="d"

Title="MainWindow" Height="350" Width="525"> <Window.DataContext>

<vm:HelloWorldViewModel /> </Window.DataContext>

... </Window>

コードビハインドをして

DataContext

をすることもできますが、

XAML IntelliSense

はなりともか

わいいことにしてください。

XAML

でにされた

DataContext

を

IntelliSense

でして、バインディング

にできるプロパティをするがあります。

/// <summary>

/// Interaction logic for MainWindow.xaml /// </summary>

public partial class MainWindow : Window {

public MainWindow() {

InitializeComponent();

DataContext = new HelloWorldViewModel(); } }

よりなで

DataContext

をするのにつフレームワークがありますが

MVVM Light

に

は

コントロールの

をするビューモデルロケータがあります、このチュートリアルのではクイックメソッドとダーテ

ィメソッドをします。

WPF

のほとんどすべてのビジュアルにして

DataContext

をできます。

DataContext

は、にオーバ

ーライドされているをいて、にビジュアルツリーのからされます

ContentPresenter

。

INotifyPropertyChanged

の

INotifyPropertyChanged

は、プロパティがされたことをユーザーインターフェイスまたはのコンポ

ーネントにらせるために、ソースつまり

DataContext

をバインドするにされるインターフェイス

INotifyPropertyChanged

(35)

。

PropertyChanged

イベントがしたことを

WPF

がに

UI

をします。すべてのビューモデルができるク

ラスにこのインタフェースをすることがましいです。

C6

では、これでなのはのとおりです。

public abstract class ViewModelBase : INotifyPropertyChanged {

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged([CallerMemberName] string name = null) {

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } }

これにより、

NotifyPropertyChanged

を2つのなるでびすことができます。

NotifyPropertyChanged()

。

CallerMemberName

というのおかげで、びすセッターのイベント

をさせます。

1.

NotifyPropertyChanged(nameof(SomeOtherProperty))

、これは

SomeOtherProperty

のイベントを

させます。

2.

C5.0

をしている.

NET 4.5

では、これをわりにできます。

public abstract class ViewModelBase : INotifyPropertyChanged {

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged([CallerMemberName] string name = null) {

var handler = PropertyChanged; if (handler != null)

{

handler(this, new PropertyChangedEventArgs(name)); } } }

4.5

よりの.

NET

のバージョンでは、プロパティをまたは

をしてするがあります

。

INotifyPropertyChanged

をしていない "のい

C

オブジェクト"

POCO

のプロパティにバインドし、バイ

ンディングがどおりにすることをすることは

INotifyPropertyChanged

。これは.

NET

のしであり、お

そらくけなければなりません。に、バインディングの

Mode

が

OneTime

ここを

OneTime

、メモリリー

クがします。

INotifyPropertyChanged

をせずにバインディングをするのはなぜですか

のきのプロパティにバインドする

(36)

<StackPanel>

<CheckBox x:Name="MyCheckBox" IsChecked="True" />

<TextBlock Text="{Binding IsChecked, ElementName=MyCheckBox}" /> </StackPanel>

のにする

RelativeSource

バインディングをすると、ビジュアルツリーののプロパティにバインドできます。

ビジュアルツリーの、じタイプの、またはしたタイプからしたもいもいコントロールがバインデ

ィングのソースとしてされます。

<Grid Background="Blue">

<Grid Background="Gray" Margin="10"> <Border Background="Red" Margin="20">

<StackPanel Background="White" Margin="20">

<Button Margin="10" Content="Button1" Background="{Binding Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}}" /> <Button Margin="10" Content="Button2" Background="{Binding Background,

RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FrameworkElement}}}" /> </StackPanel>

</Border> </Grid> </Grid>

このでは、もい

Grid

がのをつため、

Button1

はのをっています。

Button2

は、いをってい

StackPanel

。これは、

FrameworkElement

からしたもいがい

StackPanel

です。

マルチバインディングでのをバインドする

MultiBinding

では、のをじプロパティにバインドできます。のでは、のが

Textbox

の

Text

プロパテ

ィにバインドされ、

StringFormat

プロパティをしてされています。

<TextBlock> <TextBlock.Text> <MultiBinding StringFormat="{}{0} {1}"> <Binding Path="User.Forename"/> <Binding Path="User.Surname"/> </MultiBinding> </TextBlock.Text> </TextBlock>

(37)

StringFormat

ほかに、

IMultiValueConverter

して、

Bindings

のを

MultiBinding

のターゲットの1つのに

することもできます。

ただし、

MultiBinding

をネストすることはできません。

オンラインでWPFデータバインディングのをむ

https://riptutorial.com/ja/wpf/topic/2236/wpf

データ

バインディングの

(38)

6: WPFでスプラッシュスクリーンをする

き

WPF

アプリケーションをすると、のランタイム

CLR

で.

NET Framework

をするのにがかかること

があります。として、のアプリケーションウィンドウは、アプリケーションのさにじて、アプリ

ケーションのしばらくされることがあります。

WPF

のスプラッシュでは、のウィンドウがされる

に、にイメージまたはカスタムコンテンツのいずれかをすることができます。

Examples

シンプルなスプラッシュの

Visual Studio

の

WPF

アプリケーションにスプラッシュスクリーンをするには、のにいます。

をまたはしてプロジェクトにする

Images

フォルダなど

1.

(39)

このイメージの「プロパティーをく」ウィンドウ

「」→「プロパティー」ウィンドウ

、「

ビルド・アクション」のを「 スプラッシュ・スクリーン」のにします。

(40)

アプリケーションをします。アプリケーションウィンドウがされるに、にスプラッシュイメ

ージがされますウィンドウがされた、スプラッシュイメージは

300

ミリにえます。

3.

スプラッシュのテスト

アプリケーションがでシンプルであれば、ににし、のでスプラッシュがされえます。

Application.Startup

メソッドがしたにスプラッシュがえると、のでアプリケーションのをシミュレ

ートできます。

App.xaml.cs

ファイルをく

1.

using System.Threading;

2.

(41)

をして

using System.Threading;

します

using System.Threading;

OnStartup

メソッドをオーバーライドし、

Thread.Sleep(3000);

をし

Thread.Sleep(3000);

その

3.

コードはのようになります。

using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading; using System.Windows;

参照

関連したドキュメント

This function greatly simplifies the design of the auxiliary supply and the V cc capacitor by activating the internal startup current source to supply the controller during

This function greatly simplifies the design of the auxiliary supply and the V CC capacitor by activating the internal start-up current source to supply the controller during

Figure 7.. Current Sense Resistor and Peak Current Limit The inductor current is sensed by a current sense resistor in series with the inductor. The sense resistor value configures

The example problems studied include the standard overlapping scans, a multiple searcher with multiple objects problem where each searcher is tuned to a specific object type, and

As a consequence, in a homological category with finite sums, the ternary co-smash product functors preserve regular epimorphisms, as do the binary ones (see Corollary 2.14). Section

The analog to digital converter is a 7−bit A/D which can be used as an event recorder, an input voltage sampler, output voltage sampler, input current sampler, or output

This position, presupposing the same structure of the saMskAra-karman as in the previous position, regards the capacity (sAmarthya) or the effect produced through the

What is ascertained (ni´sc¯ıyate) is the object of conceptual cognitions ( vikalpavis.aya ). But an internal image is not ascertained. Therefore, it cannot be the object of a