1

I'm trying to convert some XAML to C# code. Here's the xaml:

<ComboBox TextBlock.Foreground="{Binding DesiredForegroundBrush}"/>

I'd like to do the same thing in C# code, but I'm at a loss on how to access the TextBlock.

I tried the following:

 ComboBoxInstance.TextBlock.SetBinding(TextBlock.ForegroundProperty, "DesiredForegroundBrush");

But the TextBlock is not accessible in C# code.

I also tried getting to the child of the combo box, but the GetChildrenCount returns 0:

 var childrenCount = VisualTreeHelper.GetChildrenCount(ComboBoxInstance);

I did a few web searches but all I found was questions about how to bind combo boxes to TextBoxes.

I feel like there has to be an easy way to do this. Any help would be appreciated!

Update:

I have found this post:

How to I access an attached property in code behind?

But that only shows how to directly assign the property in the code behind, as opposed to set up binding on it.

Community
  • 1
  • 1
Victor Chelaru
  • 4,491
  • 3
  • 35
  • 49
  • 1
    You can try `comboBoxTest.SetBinding( TextBlock.ForegroundProperty, new Binding( "DesiredForegroundBrush" ) );` Are there some problems with this? – bars222 Jan 29 '16 at 05:46

2 Answers2

2

use ComboBox.ForegroundProperty to bind the foreground Color. Why do you want the textbox ?

0

Every control derived from "Control" class will have Foreground property which is a dependency property and could be bound to any Brush value. To change the value of any control foreground we don't need to dig for the property.

<ComboBox Background="{Binding ForegroundColorBrush}"/>

and combobox foreground color will change according to binding.

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66