JavaFX ColorPicker能否获取“新颜色”值?
Hey there! No need to apologize for your English—your question is totally clear. The issue you're facing is that your current event handler only triggers when the ColorPicker closes, but you want to capture the "nouvelle couleur" value as soon as the user selects it, right?
Here's how to fix this for common UI frameworks:
WPF ColorPicker Solution
If you're using WPF (either the built-in ColorPicker or the one from the WPF Toolkit), swap your current event with the SelectedColorChanged event. This event fires every time the user adjusts the color—whether they pick from the palette, drag a slider, or enter a hex code—so you get the updated value instantly, no need to close the picker:
// XAML setup <ColorPicker x:Name="MonColorPicker" SelectedColorChanged="MonColorPicker_CouleurSelectionneeModifiee" /> // Code-behind private void MonColorPicker_CouleurSelectionneeModifiee(object sender, RoutedPropertyChangedEventArgs<Color?> e) { if (e.NewValue.HasValue) { Color nouvelleCouleur = e.NewValue.Value; // Use the new color immediately here! Console.WriteLine($"Nouvelle couleur: {nouvelleCouleur.ToString()}"); } }
WinUI 3 ColorPicker Solution
For WinUI 3, use the ColorChanged event instead—it works exactly the same way, firing in real-time as the user modifies the color:
// XAML setup <ColorPicker x:Name="MonColorPickerWinUI" ColorChanged="MonColorPickerWinUI_CouleurModifiee" /> // Code-behind private void MonColorPickerWinUI_CouleurModifiee(ColorPicker sender, ColorChangedEventArgs args) { Color nouvelleCouleur = args.NewColor; // Access the updated color right away Debug.WriteLine($"Nouvelle couleur: {nouvelleCouleur}"); }
Quick Tip
The problem with your current setup is that you're using an event tied to the picker closing (like Closed or a confirmation button click). By switching to a color-specific change event, you'll capture the "nouvelle couleur" value the moment it's selected, without waiting for the picker to close.
If you're using a different framework (like Avalonia, Blazor, or a third-party library), feel free to share more details and I can help tailor the solution to your setup!
内容的提问来源于stack exchange,提问作者Antoine

