C# 如何在C中绑定ComboBox.TextBlock#

C# 如何在C中绑定ComboBox.TextBlock#,c#,wpf,data-binding,combobox,C#,Wpf,Data Binding,Combobox,我正在尝试将一些XAML转换为C代码。以下是xaml: <ComboBox TextBlock.Foreground="{Binding DesiredForegroundBrush}"/> 但是文本块在C代码中是不可访问的 我还尝试访问组合框的子对象,但GetChildrenCount返回0: var childrenCount = VisualTreeHelper.GetChildrenCount(ComboBoxInstance); 我做了一些网络搜索,但我发现的只是关于

我正在尝试将一些XAML转换为C代码。以下是xaml:

<ComboBox TextBlock.Foreground="{Binding DesiredForegroundBrush}"/>
但是文本块在C代码中是不可访问的

我还尝试访问组合框的子对象,但GetChildrenCount返回0:

 var childrenCount = VisualTreeHelper.GetChildrenCount(ComboBoxInstance);
我做了一些网络搜索,但我发现的只是关于如何将组合框绑定到文本框的问题

我觉得必须有一个简单的方法来做到这一点。任何帮助都将不胜感激

更新:

我发现这个帖子:


但这只显示了如何在代码隐藏中直接分配属性,而不是在其上设置绑定。

使用
ComboBox.ForegroundProperty
绑定前景色。为什么要使用文本框?

从“control”类派生的每个控件都将具有前台属性,该属性是一个依赖属性,可以绑定到任何画笔值。要更改任何控件前景的值,我们不需要挖掘属性

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


comboBoxTest.SetBinding(TextBlock.ForegroundProperty,新绑定(“DesiredForegroundBrush”);ComboxBox的前景色将根据绑定的不同而变化。

您可以尝试
comboBoxTest.SetBinding这有什么问题吗?
<ComboBox Background="{Binding ForegroundColorBrush}"/>