WPF binding not worked for foreground property in TextBlock - c#

I have following code to set the foreground property
TextBlock label = new TextBlock() { Style = LabelStyle };
Binding binding = new Binding(LabelColor) { Source = dataContextProperty };
label.SetBinding(TextBlock.ForegroundProperty, binding);
Note that LabelColor is a Brush.
If I set,
label.SetBinding(TextBlock.BackgroundProperty, binding)
It worked. So why it is not working only for foreground?

Related

Binding ComboBox (source) with Buttons not working properly.

Hello I am binding 16 buttons to combobox, but I have problem that it works as it should only when window is loaded, then when i pick new color from ComboBox, background of buttons don't change.
This is how I am binding (Shape1Color is said combobox):
for (int i = 0; i < Shape1.Children.Count; i++)
{
Binding btnbinding = new Binding();
btnbinding.Converter = new ButtonColorConverter();
btnbinding.Source = Shape1Color.SelectedItem;
btnbinding.NotifyOnSourceUpdated = true;
(Shape1.Children[i] as Button).SetBinding(Button.BackgroundProperty, btnbinding);
}
So it only works when window loads, but then when i choose new item from combobox it doesn't enter my converter and I don't know why.
try use Shape1Color as binding Source and set SelectedItem as Path for binding
Binding btnbinding = new Binding();
btnbinding.Converter = new ButtonColorConverter();
btnbinding.Source = Shape1Color;
btnbinding.Path = new PropertyPath("SelectedItem");
btnbinding.NotifyOnSourceUpdated = true;
setting btnbinding.Source = Shape1Color.SelectedItem; works but only once. It doesn't change when SelectedItem changes

wpf dynamic border thickness binding

I have a border generated dynamically.
I want to bind thickness property of it, with my style model.
Border db = new Border();
Binding borderthickness = new Binding();
borderthickness.Source = this.imodel.GridStyleProperties.BorderThickness;
borderthickness.Path = new PropertyPath("BorderThickness");
BindingOperations.SetBinding(db, Border.BorderThicknessProperty, borderthickness);
This is what I have done so far. Its not working for me.
Please suggest me what is wrong with this code.
Thank you
You are binding to a BorderThickness object, with Path set to BorderThickness. That means the binding will look in this.imodel.GridStyleProperties.BorderThickness.BorderThickness which doesn't exist.
Try
borderthickness.Source = this.imodel.GridStyleProperties;
borderthickness.Path = new PropertyPath("BorderThickness");
or just
borderthickness.Source = this.imodel.GridStyleProperties.BorderThickness;

Cell Style Binding does not Update on Value Change

I have a DataGrid which is bound to a Collection of Objects. Every DataGridColumn is created in code behind.
The Background of these columns is dependent on different values of the object. I create the background binding in the CellStyle (as it should not override default style's from triggers).
var backgroundBinding = new Binding
{
Converter = new MyBindingConverter(),
ConverterParameter = new MyConverterParameter()
};
cellStyle.Setters.Add(new Setter(Control.BackgroundProperty, backgroundBinding));
As you can see it binds directly to the element. As different value are changing the value of the Columns is updated accordingly, but the converter of the Binding is not called.
I tried calling OnPropertyChanged(null) to show that the object was updated, but sadly this does not work.
Did you try to specify Path for backgroundBinding? Something like:
var backgroundBinding = new Binding
{
Converter = new MyBindingConverter(),
ConverterParameter = new MyConverterParameter(),
ElementName = YourElementName,
Path = PropertyOnElement
};
cellStyle.Setters.Add(new Setter(Control.BackgroundProperty, backgroundBinding));

Binding a user control text property in code-behind

I'm trying to create a custom control which inherits from a TextBox control. in the constructor of my custom control I need to have the text property bound. this the code I'm using by now.
var binding = new Binding("Name");
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
binding.ValidatesOnDataErrors = true;
SetValue(TextProperty, binding);
I haven't set a source for binding because I want it be the DataContext of the form on which my custom control will be placed however I suppose I'm doing it in a wrong way because when I added on the form I face the error message in xaml which says: "System.Windows.Data.Binding is not a valid value for property text.
Its not good to do in code-behind, hope this answers your question
Binding binding = new Binding();
binding.Path = new PropertyPath("yourPropertyTobeBinded");
binding.Source = sourceObject;
BindingOperations.SetBinding(youTextBox, TextBox.TextProperty, binding);

Get Binding from a FrameworkElementFactory

In a GirdView the text of some columns sould be aligned to the right.
To do that I create a DataTemplate, which contains a TextBlock.
Binding bd = new Binding("path");
FrameworkElementFactory tbContent = new FrameworkElementFactory(typeof(TextBlock));
tbContent.SetBinding(TextBlock.TextProperty, bd);
tbContent.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
DataTemplate dataTemplate = new DataTemplate();
dataTemplate.VisualTree = tbContent;
myGridViewColumn.CellTemplate = dataTemplate;
In an other class I have to access the Bindings of my GridViewColumns. How can I access the Binding of this column?
I had the same issue, so I exposed the TextAlignmentProperty as a public property.

Categories