question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to define the code behind Converter for Style applied in Button Control in UWP?

See original GitHub issue

I 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, CodeBehindConverter

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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisgleincommented, Nov 18, 2021

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.

0reactions
StephenLPeterscommented, Oct 26, 2021

@chrisglein you may have tried something like this during your experiment?

Read more comments on GitHub >

github_iconTop Results From Across the Web

define a style for a button in code behind UWP app
Is there a particular reason you are trying to do this versus creating a resource dictionary? I have never seen this approach.
Read more >
{x:Bind} markup extension - UWP applications
You can explicitly specify a mode to use OneWay or TwoWay binding. ... So it will look in the code-behind of your page...
Read more >
Tutorial: Create data bindings - UWP applications
This means you can reference anything you put in code-behind or declare in XAML, including custom properties and properties of other UI controls...
Read more >
Data binding via Code-behind
Data binding via Code-behind. As we saw in the previous data binding examples, defining a binding by using XAML is very easy, but...
Read more >
XAML Anti-Patterns: Code-Behind
As you can see, defining a button with a richly formatted caption is ... Applying proper code-reuse and styling techniques, you can create ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found