Welcome to the Xceed Community Sign in | Join | Help
Community Search  

Help with multibinding

Sort Posts: Previous Next
  •  09-06-2008, 8:39 AM Post no. 14966

    Help with multibinding

    I'm trying to use a converter to set the foreground color for text in the grid.  The reason I'm doing this is to overcome a problem with using a Style to set the color, it's not working reliably.

    I have this code to call the converter:

                            <MultiBinding Converter="{StaticResource setColor}">
                                <MultiBinding.Bindings>
                                    <Binding ElementName="FieldName" Path="Categories" />
                                    <Binding ElementName="Content" Path="Value" />
                                </MultiBinding.Bindings>
                            </MultiBinding>

     

    but I'm not sure where to "put" it... I tried it inside a Style for the DataCell but get a runtime error that multibindings aren't supported in Styles.

    Any ideas? Thanks!

    BTW here's the resource definition (inside Window.Resources):

            <local:cvtColor x:Key="setColor" />


    and the converter:


        public class cvtColor : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string colname = (string)(values[0]);
                string text = (string)(values[1]);

                if (colname == "Categories")
                {
                    switch (text)
                    {
                        case "blacktext": return Brushes.Black;
                        case "greentext": return Brushes.Green;
                        case "yellowtext": return Brushes.Yellow;
                        case "orangetext": return Brushes.Orange;
                    }
                }
                return Brushes.Black;
            }

            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotSupportedException("Not supported");
            }
        } 
     


  •  09-08-2008, 11:05 AM Post no. 15012 in reply to 14966

    Re: Help with multibinding

    Try something like this:

    <local:CountryToColorConverter x:Key="countryToColorConverter"/>

    <Style TargetType="{x:Type xcdg:DataCell}">
      <Setter Property="Foreground"
              Value="{Binding Converter={StaticResource countryToColorConverter}}"/>           
    </Style>

      public class CountryToColorConverter : IValueConverter
      {
        public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
          if( value == null )
            return Brushes.Black;

          System.Data.DataRow row = value as System.Data.DataRow;

          if( row != null )
          {
            if( ( string )row[ "ShipCountry" ] == "Canada" )
            {
              return Brushes.HotPink;
            }

            if( ( string )row[ "ShipCountry" ] == "Brazil" )
            {
              return Brushes.Green;
            }
          }

          return Brushes.Black;
        }

        public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
          return Binding.DoNothing;
        }
      }


    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
  •  09-08-2008, 11:49 AM Post no. 15019 in reply to 15012

    Got it

    and it works.

  •  09-09-2008, 11:52 AM Post no. 15078 in reply to 15019

    BTW this approach fixed another issue

    Using a converter instead of a bunch of datatriggers solved the problem I was having of styles not being applied consistently...
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.