How to define the code behind Converter for Style applied in Button Control in UWP?
See original GitHub issueI have tried to fully code behind display control in the UWP application. While Applying Style in Button control I need to use the Converter for apply style in Button control. But Unfortunately Converter not triggered for Button Control.
Here is my attempt to recreate it in CodeBehind, but it is unfortunately not working.
//create converter instance
var converterInstance = new BoolToColorConverter();
//add the converter to window resources
this.Resources.Add("BoolToColorConverter", converterInstance);
Binding myBinding = new Binding();
myBinding.Converter = (BoolToColorConverter)this.Resources["BoolToColorConverter"];
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
Style buttonStyle = new Style(typeof(Button));
buttonStyle.Setters.Add(new Setter(Button.BackgroundProperty, myBinding));
Button button = new Button();
button.Width = 150;
button.Height = 50;
button.Content = "Hello";
button.Style = buttonStyle;
parent.Children.Add(button);
Screenshot for your reference,
I have attached a sample for your reference.
Sample Link: CodeBehindinConverter.zip
If I missed anything in this code behind.
Can you provide how to code-behind Converter for Style applied in Button Control as targetType in UWP platform?
Note: My requirement is needed to apply Style using converter in mentioned target type via code behind in UWP Platform
Regards, Vijayarasan S
Note: Please refer to the MSDN link: https://developercommunity.visualstudio.com/t/how-to-define-the-code-behind-converter-for-style/1382127
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
I didn’t hit anything quite like this, mainly because I avoided any Converters entirely. If you’re going towards code-first instead of markup-first Converters don’t make much sense; they’re a lot of overhead/complexity compared to doing the conversions inline as you would with normal code.
@chrisglein you may have tried something like this during your experiment?