locked
Binding foregrounds using IMultiValueConverter RRS feed

  • שאלה

  • Hello Dear friends,

    I'll appreciate your help in the next problem :

    I have 2 TextBoxes (i.e. X1,X2) binding to 1 TextBox (i.e. Y) so whenever X1's or X2's foreground changes Y's foreground will change also to the same color and vice versa.

    In my program, at first, all TextBoxes' foregrounds are Green and when the user insert a key to one of them it's foreground changes to Red (and the binded textBoxes should turn Red also).

    The problem is that one side of the binding is working fine but the other side does nothing (meaning when the user inserts a key to Y all X1,X2,Y TextBoxes' foregrounds stay Green - which is not the desirable behaviour).

    I have the short code with the problem attached.


    Thank you very much, appreciate your time and effort!

    Gil.                          

    preview of the code:

    namespace MyProblem { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } new private void PreviewTextInput(object sender, TextCompositionEventArgs e) { ((TextBox)sender).Foreground = new SolidColorBrush(Colors.Red); } private void Button_Click(object sender, RoutedEventArgs e) { TB_X1.Foreground = new SolidColorBrush(Colors.Green); TB_X2.Foreground = new SolidColorBrush(Colors.Green); } } } namespace MyConverter { class X1AndX2ToYColorConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (((System.Windows.Media.SolidColorBrush)values[0]).Color == Colors.Red || ((System.Windows.Media.SolidColorBrush)values[1]).Color == Colors.Red) { return new SolidColorBrush(Colors.Red); } else { return new SolidColorBrush(Colors.Green); } } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { if (((System.Windows.Media.SolidColorBrush)value).Color == Colors.Red) { SolidColorBrush t1 = new SolidColorBrush(Colors.Red); return new SolidColorBrush[] { t1, t1 }; } else { SolidColorBrush t2 = new SolidColorBrush(Colors.Green); return new SolidColorBrush[] { t2, t2 }; } } } }

    <Window x:Class="MyProblem.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:MyConverter"
            Title="MainWindow" Height="350" Width="442">
        <Window.Resources>
            <local:X1AndX2ToYColorConverter x:Key="X1_and_X2_to_Y_color_converter" />
        </Window.Resources>
        <Grid>
            <Label Content="X1" FontSize="14" Height="29" HorizontalAlignment="Left" Margin="128,40,0,240"/>
            <Label Content="X2" FontSize="14" Height="29" HorizontalAlignment="Left" Margin="128,95,0,187"/>
            <Label Content="Y"  FontSize="14" Height="29" HorizontalAlignment="Left" Margin="131,211,0,71"/>
            <TextBox Foreground="Green" Height="31" HorizontalAlignment="Left" Margin="178,41,0,239" Name="TB_X1" Text="0" Width="50" PreviewTextInput="PreviewTextInput"/>
            <TextBox Foreground="Green" Height="31" HorizontalAlignment="Left" Margin="178,93,0,187" Name="TB_X2" Text="0" Width="50" PreviewTextInput="PreviewTextInput"/>
            <TextBox Height="31" HorizontalAlignment="Left" Margin="178,210,0,70" Name="TB_Y" Text="0" Width="50" PreviewTextInput="PreviewTextInput">
                <TextBox.Foreground>
                    <MultiBinding Converter="{StaticResource X1_and_X2_to_Y_color_converter}" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
                        <Binding ElementName="TB_X1" Path="Foreground" />
                        <Binding ElementName="TB_X2" Path="Foreground" />
                    </MultiBinding>
                </TextBox.Foreground>
            </TextBox>
            <Button Content="Green X1, X2 (Y will get green also due to binding)" Height="32" HorizontalAlignment="Left" Margin="47,152,0,0" VerticalAlignment="Top" Width="329" Click="Button_Click"/>
        </Grid>
    </Window>

    יום חמישי 07 יוני 2012 14:35

תשובות

  • היי גיל

    הגדרת הBinding של TB_Y צריכה להעשות כך:

     <MultiBinding Converter="{StaticResource X1_and_X2_to_Y_color_converter}" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
         <Binding ElementName="TB_X1" Path="Foreground" Mode="TwoWay" />
         <Binding ElementName="TB_X2" Path="Foreground" Mode="TwoWay"/>
     </MultiBinding>

    יש להגדיר שצבע הכתב של TB_Y ישפיע גם בכיוון הנגדי -כלומר על 2 תיבות הטקסט האחרות.

    בהצלחה

    • הוצע כתשובה על-ידי Elad R Katz יום שלישי 12 יוני 2012 15:08
    • סומן כתשובה על-ידי Eran Sharvit יום שלישי 12 יוני 2012 15:37
    יום שלישי 12 יוני 2012 14:52

כל התגובות

  • Hi Gil, what happens if you put a break point at the start of ConvertBack and change Y's color? is it even called?

    (i'm sorry, this workstation has no visual studio on it, i cannot debug)

    יום שלישי 12 יוני 2012 05:33
  • היי גיל

    הגדרת הBinding של TB_Y צריכה להעשות כך:

     <MultiBinding Converter="{StaticResource X1_and_X2_to_Y_color_converter}" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
         <Binding ElementName="TB_X1" Path="Foreground" Mode="TwoWay" />
         <Binding ElementName="TB_X2" Path="Foreground" Mode="TwoWay"/>
     </MultiBinding>

    יש להגדיר שצבע הכתב של TB_Y ישפיע גם בכיוון הנגדי -כלומר על 2 תיבות הטקסט האחרות.

    בהצלחה

    • הוצע כתשובה על-ידי Elad R Katz יום שלישי 12 יוני 2012 15:08
    • סומן כתשובה על-ידי Eran Sharvit יום שלישי 12 יוני 2012 15:37
    יום שלישי 12 יוני 2012 14:52
  • תודה! שרפתי על זה יותר מדי זמן ובסוף הפתרון פשוט כמו שצריך להיות :)
    יום שלישי 12 יוני 2012 15:48