定数
定数使用例
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:syswin="clr-namespace:System.Windows;assembly=PresentationFramework"
<Window.Resources>
<sys:Double x:Key="Width">NaN</sys:Double>
<sys:Boolean x:Key="Enable">true</sys:Boolean>
<sys:String x:Key="Text">ボタン</sys:String>
<syswin:HorizontalAlignment x:Key="HorizontalAlignment">Left</syswin:HorizontalAlignment>
</Window.Resources>
<Label Width="{StaticResource Width}"
IsEnabled="{StaticResource Enable}"
HorizontalAlignment="{StaticResource HorizontalAlignment}">
<Label.Style>
<Style TargetType="{x:Type Label}">
<Setter Property="Content" Value="{StaticResource Text}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Source={StaticResource Width}}" Value="{x:Static sys:Double.NaN}">
<Setter Property="Content" Value="{x:Static sys:Double.NaN}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
Binding
MultiBinding
BindしているDirtyがtrueならアプリのタイトルに"*"をつける例です。
<Window.Title>
<MultiBinding StringFormat="{}{0}{1}">
<Binding Source="{StaticResource APP_TITLE}"/>
<Binding Path="Dirty" ConverterParameter=";*">
<Binding.Converter>
<converter:BooleanToTextConverter/>
</Binding.Converter>
</Binding>
</MultiBinding>
</Window.Title>
DataGrid
文字の色を変更する
以下はName欄の文字色を青にする例です。
<DataGridTextColumn Header="氏名" Binding="{Binding Name}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Foreground" Value="Blue"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
1回のクリックでチェックボックスのON/OFFを切り替える
以下はON/OFF欄の各行にチェックボックスを表示する例です。このチェックボックスは1回のクリックでON/OFFが切り替わります。
<DataGridTemplateColumn Header="ON/OFF">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding OnOff, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
1回のクリックでコンボボックスのプルダウンを展開する
IsSynchronizedWithCurrentItemをFalseにしていないと全行に同じ値が表示された。
※Trueの場合、DataGridはDataGridのItemsSourceからSelectedValueに指定した値を使用して表示しているが、最後に取得した値をコンボボックスに表示しているようだ。
<DataGridTemplateColumn Header="COMBOBOX" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding Mode=OneWay, Source={StaticResource HogeCollectionViewSource}}"
IsSynchronizedWithCurrentItem="False"
DisplayMemberPath="HogeName"
SelectedValuePath="HogeId"
SelectedValue="{Binding HogeId}"
Width="300"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
コレクション
コレクションのソート順を指定する
以下はViewModelのSampleCollectionをXAML側でName欄とValue欄でソートする例です。XAML内ではSampleCollectionをStaticResourceで利用できます。
<Window
・・・
xmlns:SystemComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
・・・>
<Window.Resources>
<CollectionViewSource x:Key="SampleCollection" Source="{Binding SampleCollection}">
<CollectionViewSource.SortDescriptions>
<SystemComponentModel:SortDescription PropertyName="Name"/>
<SystemComponentModel:SortDescription PropertyName="Value"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>