LoginSignup
0
1

More than 5 years have passed since last update.

WPFのXAMLに関するメモ

Last updated at Posted at 2019-02-03

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>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1