EmpressiaLibrary.Behaviors.UWP 1.0.2

dotnet add package EmpressiaLibrary.Behaviors.UWP --version 1.0.2
NuGet\Install-Package EmpressiaLibrary.Behaviors.UWP -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EmpressiaLibrary.Behaviors.UWP" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EmpressiaLibrary.Behaviors.UWP --version 1.0.2
#r "nuget: EmpressiaLibrary.Behaviors.UWP, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EmpressiaLibrary.Behaviors.UWP as a Cake Addin
#addin nuget:?package=EmpressiaLibrary.Behaviors.UWP&version=1.0.2

// Install EmpressiaLibrary.Behaviors.UWP as a Cake Tool
#tool nuget:?package=EmpressiaLibrary.Behaviors.UWP&version=1.0.2

Empressia Library Behaviors for UWP

このライブラリは、UWPで使用できるBehaviorを増やすものです。
主に自分用です。

Behavior一覧

以下のBehaviorを使用できます。

IsSelectedBehavior
ParentPropertyBehavior


IsSelectedBehavior

何をするの?

指定された要素を含むツリーの選択状態(SelectorItem.IsSelected)へバインドするBehaviorです。 Bindingプロパティの結果は、Behaviorに対してではなく、上位のSelectorItemに設定されます。 結果として、SelectorItemのDataContextが基準になりますが、通常、そこには何も設定されていません。 このBehaviorは初期設定状態でSelectorItemのDataContextを、 Behaviorの記載されたFrameworkElementのDataContextで上書きします。 この動作を抑制したい場合は、TransferDataContextToSelectorItemプロパティをfalseにしてください。

どういうとき使うの?

GridViewItemなどのSelectorItemのIsSelectedにバインドしたい場合に使用します。 どのクラスがSelectorItemかは、以下のサイトを参照ください。 https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.primitives.selectoritem#selectoritem-derived-classes

使い方

たとえば、以下のように使用します。
※適宜、xmlns:eui="using:jp.empressia.ui"が設定されている想定です。

<GridView ItemsSource="{x:Bind Images}" SelectionMode="Multiple">
	<GridView.ItemsPanel>
		<ItemsPanelTemplate>
			<ItemsWrapGrid Orientation="Horizontal" />
		</ItemsPanelTemplate>
	</GridView.ItemsPanel>
	<GridView.ItemTemplate>
		<DataTemplate x:DataType="local:Thumbnail">
			<RelativePanel Width="{x:Bind Width, Mode=OneWay}" Height="{x:Bind Height, Mode=OneWay}">
				<Image Source="{x:Bind Image, Mode=OneWay}">
					<Interactivity:Interaction.Behaviors>
						<eui:IsSelectedBehavior Binding="{Binding Path=IsSelected, Mode=TwoWay}" />
					</Interactivity:Interaction.Behaviors>
				</Image>
			</RelativePanel>
		</DataTemplate>
	</GridView.ItemTemplate>
</GridView>

ParentPropertyBehavior

どういうとき使うの?

ContentPresenterに添付プロパティを設定するために作られました。
ItemsControlで、Canvasの中にDataTemplateを設定したときに、Canvas.LeftやCanvas.Topを指定したくても、
ContentPresenterが間に入ってしまって有効に設定できない問題を解消することができます。

何をするの?

親要素の指定プロパティへバインドするBehaviorです。
Bindingプロパティの結果は、
Behavior、および、それを指定された要素に対してではなく、
親要素(上位のFrameworkElement)に設定されます。

使い方

たとえば、以下のように使用します。
※適宜、xmlns:eui="using:jp.empressia.ui"が設定されている想定です。

<ItemsControl ItemsSource="{x:Bind Ticks}">
	<ItemsControl.ItemsPanel>
		<ItemsPanelTemplate>
			<Canvas VerticalAlignment="Center" HorizontalAlignment="Stretch" />
		</ItemsPanelTemplate>
	</ItemsControl.ItemsPanel>
	<ItemsControl.ItemTemplate>
		<DataTemplate x:DataType="x:Int32">
			<Rectangle Width="1" Height="20" Fill="{ThemeResource SliderTrackValueFill}" VerticalAlignment="Center" Canvas.Top="100">
				<Interactivity:Interaction.Behaviors>
					<eui:ParentPropertyBehavior Property="{x:Bind local:MainPage.CanvasLeftProperty()}" Binding="{Binding}" />
				</Interactivity:Interaction.Behaviors>
			</Rectangle>
		</DataTemplate>
	</ItemsControl.ItemTemplate>
</ItemsControl>

Bindingの指定は、ContentPresenterのDataContextにItemが設定されるはずなので、そこが基準になるはずです。
Propertyには、指定したいDependencyPropertyを指定します。 関数バインドを経由させて設定すると楽かもしれません。


トラブルシュート

なんか、XAMLの解析で失敗するんだけど。

これに該当しないか確認してください。 https://github.com/Microsoft/XamlBehaviors/issues/79

更新履歴

2018/04/13 1.0.2.0 ParentPropertyBehaviorを追加しました。
2017/04/14 1.0.1.0 IsSelectedBehaviorがDataContextChangedでエラーになる問題を修正しました。
2017/04/13 1.0.0.0 IsSelectedBehaviorを追加しました。

Product Compatible and additional computed target framework versions.
Universal Windows Platform uap was computed.  uap10.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 1,169 4/15/2018
1.0.1 1,075 4/14/2017
1.0.0 1,086 4/13/2017