JohnChris JohnChris - 3 months ago 29
C# Question

One Way to Source Binding not working

Ok quick summary is, I am trying to do a onewaytosource binding.

So explanation of my understanding of the code. OneWaytoSource is the reverse of One Way binding. The target updates the source. So here my target is where I create the binding (so my textbox) my target property is the Foreground. My source is my menuItem and my source property is "IsEnabled".

The value converter is programmed in ConvertBack as this is the section that is meant for Onewaytosource or two way binding.

One of the textboxes:(I am planning to have this similar code on about 40 textboxes, which will disable my menuItem if the foreground is red)

<TextBox x:Name="upperLimitMinimum" Foreground="{Binding ElementName=menuItem, Path=IsEnabled, Mode=OneWayToSource, Converter={StaticResource myColorErrorConverter}}"

This is my code for converter:

public class myColorErrorConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

return null;

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
if (null != value)
string x = value.ToString();
if (x == "Red")
{ return true; }

return false;

My menuItem,

<MenuItem x:Name="menuItem" Header="Menu Item( Ctrl + E )" Command="{Binding MenuItem}"

What I expect to happen? When the textbox foreground turns red (which is controlled by a validation check, using a multivalue converter - not shown) The menuItem should become disabled. I have put a breakpoint on my converter and the code doesn't even enter the converter, so not really sure what is happening? Can someone shed some light on this?

After Clemens response, new code:

<DataTrigger Value="True">
<MultiBinding Converter="{StaticResource MultiValueConverterColour}" >
<Binding ElementName="upperLimitDefault" Path="Text" />
<Binding ElementName="upperLimitMaximum" Path="Text" />
<Binding ElementName="upperLimitMinimum" Path="Text" />
<Setter Property="TextBox.Foreground" Value="Red"/>

<Setter Property="TextBox.Foreground" Value="{Binding ElementName=MenuItem, Path=IsEnabled, Mode=OneWayToSource, Converter={StaticResource myColorErrorConverter}}" />

So now at start up the 2nd converter is accessed and disables/enable menuitem accordingly, but during application running, when textbox foreground changes, only the first converter is accessed and the colour will change but second converter isn't accessed


From a comment it seems that you're setting the Foreground property in a Style Setter. This assignment has a lower value precedence than the "local value" assigned by the Foreground Binding, and is therefore not effective.

You may replace

<TextBox Foreground="{Binding ...}" ... />

by a Style Setter like this (or add it to an already defined TextBox Style):

<TextBox ...>
            <Setter Property="Foreground" Value="{Binding ...}" />

For reference, see Dependency Property Value Precedence.