Hello. On the form there is an element ColorPicker, how to change the color when you click on the button. So the color does not change.

DirectionalLight dl = new DirectionalLight(); dl.Color = customCPDirectionalLight.SelectedColor; 


 private void ApplyButtonDirectionalLight_Click(object sender, RoutedEventArgs e) { win.customCPDirectionalLight.SelectedColorChanged += new Action<System.Windows.Media.Color> (customCPDirectionalLight_SelectedColorChanged); } public void customCPDirectionalLight_SelectedColorChanged(System.Windows.Media.Color obj) { MGroup.Children.Clear(); DirectionalLight dl = new DirectionalLight(); dl.Color = (System.Windows.Media.Color) obj; MGroup.Children.Add(dl); } 
  • And the color of what? - Niki-Timofe
  • There is a 3D object on the form. It has the MGroup group of light sources, one of them is DirectionalLight, how to change the color of the source when you click on the button with the help of ColrPicker. When you first click on the button, the change does not work. - Demon
  • Moved to the question. - Demon
  • dl.Color = (System.Windows.Media.Color) obj; // this color should be changed to the new one selected in colorPicker when pressing the button - Demon
  • Does DirectionalLight have exactly the Color attribute? - Niki-Timofe

2 answers 2

It would be better to tie the color of the required object to the resource and change the resource itself:

 ... <Window.Resources> <Color x:Key="colorLight">#FFFFFF</Color> </Window.Resources> ... <DirectionalLight Color="{DynamicResource colorLight}" Direction="-1,-1,-1" /> ... 

And change accordingly, for example:

 window1.Resources["colorLight"] = color; 
     Попробуйте: dl.Color = new SolidColorBrush(тут цвет);