色々見ているとTemplateを書き換えなければならない??
ほかの方法もあるかもしれませんが、手間は一番少ないと思います(xamlは爆発的に増えますが^^;
仮でいいのでxamlにComboBox
を追加してください。
デザイナ上でComboBox
を右クリックし、「テンプレートの編集」-「コピーして編集」でスタイルを出力します。
出力出来たら仮のComboBox
はもういりません。
どこかに↓ようなスタイルが出ていると思います(微妙に差があるかもしれません)
xml
1<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
2 <Setter Property="OverridesDefaultStyle" Value="true" />
3 <Setter Property="IsTabStop" Value="false" />
4 <Setter Property="Focusable" Value="false" />
5 <Setter Property="ClickMode" Value="Press" />
6 <Setter Property="Template">
7 <Setter.Value>
8 <ControlTemplate TargetType="{x:Type ToggleButton}">
9 <Border
10 x:Name="templateRoot"
11 Background="{StaticResource ComboBox.Static.Background}"
12 BorderBrush="{StaticResource ComboBox.Static.Border}"
13 BorderThickness="{TemplateBinding BorderThickness}"
14 SnapsToDevicePixels="true">
xml
1Background="{StaticResource ComboBox.Static.Background}"
ここを
xml
1Background="{TemplateBinding Background}"
このように変更します。
DataGridTemplateColumn
のComboBox
のStyle
を、
xml
1<Style TargetType="ComboBox" BasedOn="{StaticResource ComboBoxStyle1}">
のように今出力したスタイルを受け継ぐようにします。
これでおそらくBackground
の変更が反映されると思います。
検証コード追記
xml
1<Window
2 x:Class="Questions352305.MainWindow"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:local="clr-namespace:Questions352305"
6 xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
7 Width="800"
8 Height="450">
9 <Window.Resources>
10 <x:Array x:Key="ModeToComboBoxGrps" Type="{x:Type local:ModeToComboBoxGrp}">
11 <local:ModeToComboBoxGrp Label="0:TOP" Value="0" />
12 <local:ModeToComboBoxGrp Label="1:LOW" Value="1" />
13 </x:Array>
14
15 <!-- いろいろ省略 -->
16 <!--<Style x:Key="FocusVisual" />
17 <SolidColorBrush x:Key="TextBox.Static.Background" Color="#FFFFFFFF" />
18 <Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}" />-->
19
20 <!--<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
21 <Setter Property="OverridesDefaultStyle" Value="true" />
22 <Setter Property="IsTabStop" Value="false" />
23 <Setter Property="Focusable" Value="false" />
24 <Setter Property="ClickMode" Value="Press" />
25 <Setter Property="Template">
26 <Setter.Value>
27 <ControlTemplate TargetType="{x:Type ToggleButton}">
28 <Border
29 x:Name="templateRoot"
30 Background="{TemplateBinding Background}"
31 BorderBrush="{StaticResource ComboBox.Static.Border}"
32 BorderThickness="{TemplateBinding BorderThickness}"
33 SnapsToDevicePixels="true">-->
34 <!-- 以下も省略 -->
35
36 <!-- いろいろ省略 -->
37 <!--<ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}"/>
38 <ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}"/>
39 <Style x:Key="ComboBoxStyle1" TargetType="{x:Type ComboBox}"/>-->
40 </Window.Resources>
41 <DockPanel>
42 <ComboBox
43 Background="Red"
44 DockPanel.Dock="Top"
45 Style="{DynamicResource ComboBoxStyle1}" />
46 <Button
47 Click="Button2_Click"
48 Content="更新"
49 DockPanel.Dock="Top" />
50 <DataGrid
51 AutoGenerateColumns="False"
52 ItemsSource="{Binding Records}"
53 SelectionUnit="CellOrRowHeader">
54 <DataGrid.Columns>
55
56 <DataGridComboBoxColumn
57 DisplayMemberPath="Label"
58 Header="ComboBoxColumn"
59 ItemsSource="{StaticResource ModeToComboBoxGrps}"
60 SelectedValueBinding="{Binding Mode}"
61 SelectedValuePath="Value">
62 <DataGridComboBoxColumn.CellStyle>
63 <Style TargetType="DataGridCell">
64 <Style.Triggers>
65 <DataTrigger Binding="{Binding IsModeChange}" Value="True">
66 <Setter Property="Background" Value="LightSalmon" />
67 </DataTrigger>
68 </Style.Triggers>
69 </Style>
70 </DataGridComboBoxColumn.CellStyle>
71 </DataGridComboBoxColumn>
72
73 <DataGridTemplateColumn Header="Test">
74 <DataGridTemplateColumn.CellTemplate>
75 <DataTemplate>
76 <ComboBox
77 DisplayMemberPath="Label"
78 ItemsSource="{StaticResource ModeToComboBoxGrps}"
79 SelectedValue="{Binding Mode, UpdateSourceTrigger=PropertyChanged}"
80 SelectedValuePath="Value">
81 <ComboBox.Style>
82 <Style BasedOn="{StaticResource ComboBoxStyle1}" TargetType="ComboBox">
83 <Style.Triggers>
84 <DataTrigger Binding="{Binding IsModeChange}" Value="True">
85 <Setter Property="Background" Value="LightSalmon" />
86 </DataTrigger>
87 </Style.Triggers>
88 </Style>
89 </ComboBox.Style>
90 </ComboBox>
91 </DataTemplate>
92 </DataGridTemplateColumn.CellTemplate>
93 </DataGridTemplateColumn>
94 </DataGrid.Columns>
95 </DataGrid>
96 </DockPanel>
97</Window>
cs
1using System.Collections.Generic;
2using System.ComponentModel;
3using System.Runtime.CompilerServices;
4using System.Windows;
5
6namespace Questions352305
7{
8 public class Record : Observable
9 {
10 public int Mode
11 {
12 get => _Mode;
13 set { if (SetProperty(ref _Mode, value)) IsModeChange = true; }
14 }
15 private int _Mode;
16
17 public bool IsModeChange { get => _IsModeChange; set => SetProperty(ref _IsModeChange, value); }
18 private bool _IsModeChange;
19 }
20
21 public class ModeToComboBoxGrp
22 {
23 public string Label { get; set; }
24 public int Value { get; set; }
25 }
26
27 public partial class MainWindow : Window
28 {
29 public List<Record> Records { get; } = new List<Record>();
30
31 public MainWindow()
32 {
33 InitializeComponent();
34 DataContext = this;
35
36 Records.Add(new Record());
37 Records.Add(new Record());
38 Records.Add(new Record());
39 }
40
41 private void Button2_Click(object sender, RoutedEventArgs e)
42 {
43 foreach (var record in Records)
44 {
45 record.IsModeChange = false;
46 }
47 }
48 }
49
50 public class Observable : INotifyPropertyChanged
51 {
52 public event PropertyChangedEventHandler PropertyChanged;
53 protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
54 {
55 if (Equals(storage, value)) return false;
56 storage = value;
57 OnPropertyChanged(propertyName);
58 return true;
59 }
60 protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
61 }
62}
