JohnChris JohnChris - 13 days ago 6
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:

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

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


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

Answer

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 ...>
    <TextBox.Style>
        <Style>
            <Setter Property="Foreground" Value="{Binding ...}" />
        </Style>
    </TextBox.Style>
</TextBox>

For reference, see Dependency Property Value Precedence.

Comments