Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. Bind to converter in XAML

Bind to converter in XAML

Scheduled Pinned Locked Moved WPF
wpfwcfhelp
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Madmatt
    wrote on last edited by
    #1

    Hey all, I'm running into a problem when binding a property of a GroupItemTemplate (in the WP7 LongListSelector) to a converter I built. I want the converter to check if there are items to be displayed in the group, and according to if there are items, set a Brush to the background. This is my code for the Converter:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    if (value is Group<TrackInfo>)
    {
    Group<TrackInfo> group = value as Group<TrackInfo>;
    if (group != null)
    {
    if (group.Items.Count == 0)
    return (SolidColorBrush)Application.Current.Resources["PhoneChromeBrush"];
    else
    return (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
    }
    }

            return new SolidColorBrush(Color.FromArgb(128, 255, 0, 0));
        }
    

    And this is in my XAML:

    M A 2 Replies Last reply
    0
    • _ _Madmatt

      Hey all, I'm running into a problem when binding a property of a GroupItemTemplate (in the WP7 LongListSelector) to a converter I built. I want the converter to check if there are items to be displayed in the group, and according to if there are items, set a Brush to the background. This is my code for the Converter:

      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
      if (value is Group<TrackInfo>)
      {
      Group<TrackInfo> group = value as Group<TrackInfo>;
      if (group != null)
      {
      if (group.Items.Count == 0)
      return (SolidColorBrush)Application.Current.Resources["PhoneChromeBrush"];
      else
      return (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
      }
      }

              return new SolidColorBrush(Color.FromArgb(128, 255, 0, 0));
          }
      

      And this is in my XAML:

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      You need to have the converter instance in resources somewhere visible to the datatemplate (just like the other static resources you are binding to) so the StaticResource binding works.

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      modified on Wednesday, July 13, 2011 5:58 PM

      _ 1 Reply Last reply
      0
      • _ _Madmatt

        Hey all, I'm running into a problem when binding a property of a GroupItemTemplate (in the WP7 LongListSelector) to a converter I built. I want the converter to check if there are items to be displayed in the group, and according to if there are items, set a Brush to the background. This is my code for the Converter:

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
        if (value is Group<TrackInfo>)
        {
        Group<TrackInfo> group = value as Group<TrackInfo>;
        if (group != null)
        {
        if (group.Items.Count == 0)
        return (SolidColorBrush)Application.Current.Resources["PhoneChromeBrush"];
        else
        return (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
        }
        }

                return new SolidColorBrush(Color.FromArgb(128, 255, 0, 0));
            }
        

        And this is in my XAML:

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        Try by making this line - ; part of the Itemsources or UserControls resource.

        _ 1 Reply Last reply
        0
        • M Mark Salsbery

          You need to have the converter instance in resources somewhere visible to the datatemplate (just like the other static resources you are binding to) so the StaticResource binding works.

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          modified on Wednesday, July 13, 2011 5:58 PM

          _ Offline
          _ Offline
          _Madmatt
          wrote on last edited by
          #4

          The DataTemplate as well as the Converter are in the

          <DataTemplate x:Key="groupItemTemplate">
                  <Border Width="99" Height="99" Background="{Binding Converter={StaticResource groupItemBackgroundBrush}}" Margin="6" IsHitTestVisible="{Binding HasItems}">
                      <TextBlock Text="{Binding GroupTitle}" 
                                             FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                                             FontSize="36"
                                             Margin="{StaticResource PhoneTouchTargetOverhang}"
                                             Foreground="{StaticResource PhoneForegroundBrush}"                                        
                                             VerticalAlignment="Bottom"/>
                  </Border>
              </DataTemplate>
              
              <valueConverters:TrackGroupToGroupItemBackgroundBrushConverter x:Key="groupItemBackgroundBrush" />
          </phone:PhoneApplicationPage.Resources>
          

          Is there something wrong in here?

          P S 2 Replies Last reply
          0
          • A Abhinav S

            Try by making this line - ; part of the Itemsources or UserControls resource.

            _ Offline
            _ Offline
            _Madmatt
            wrote on last edited by
            #5

            They are both part of the <phone:PhoneApplicationPage.Resources>, I cannot create a <DataTemplate.Resources>, so where should I put it else? This is quite confusing to me, it's the first time is use XAML binding in such a complex way... Thanks

            1 Reply Last reply
            0
            • _ _Madmatt

              The DataTemplate as well as the Converter are in the

              <DataTemplate x:Key="groupItemTemplate">
                      <Border Width="99" Height="99" Background="{Binding Converter={StaticResource groupItemBackgroundBrush}}" Margin="6" IsHitTestVisible="{Binding HasItems}">
                          <TextBlock Text="{Binding GroupTitle}" 
                                                 FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                                                 FontSize="36"
                                                 Margin="{StaticResource PhoneTouchTargetOverhang}"
                                                 Foreground="{StaticResource PhoneForegroundBrush}"                                        
                                                 VerticalAlignment="Bottom"/>
                      </Border>
                  </DataTemplate>
                  
                  <valueConverters:TrackGroupToGroupItemBackgroundBrushConverter x:Key="groupItemBackgroundBrush" />
              </phone:PhoneApplicationPage.Resources>
              

              Is there something wrong in here?

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              _Madmatt wrote:

              Background="{Binding Converter={StaticResource groupItemBackgroundBrush}}"

              The problem here is that you haven't told the converter what it should bind to. Try changing this to be

              "{Binding Path=., Converter={StaticResource groupItemBackgroundBrush}}"

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              _ 1 Reply Last reply
              0
              • P Pete OHanlon

                _Madmatt wrote:

                Background="{Binding Converter={StaticResource groupItemBackgroundBrush}}"

                The problem here is that you haven't told the converter what it should bind to. Try changing this to be

                "{Binding Path=., Converter={StaticResource groupItemBackgroundBrush}}"

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                _ Offline
                _ Offline
                _Madmatt
                wrote on last edited by
                #7

                No, still getting the exception with this setting... :(

                P 1 Reply Last reply
                0
                • _ _Madmatt

                  No, still getting the exception with this setting... :(

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  Put a breakpoint in your converter and see if it gets hit. Then step over the code to see what breaks.

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  _ 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Put a breakpoint in your converter and see if it gets hit. Then step over the code to see what breaks.

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    _ Offline
                    _ Offline
                    _Madmatt
                    wrote on last edited by
                    #9

                    It's not even hitting the converter! :( Not with this XAML:

                    phone:PhoneApplicationPage.Resources
                    <DataTemplate x:Key="groupItemTemplate">
                    <Border Width="99" Height="99" Background="{Binding Path=., Converter={StaticResource groupItemBackgroundBrush}}" Margin="6" IsHitTestVisible="{Binding HasItems}">
                    <TextBlock Text="{Binding GroupTitle}"
                    FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                    FontSize="36"
                    Margin="{StaticResource PhoneTouchTargetOverhang}"
                    Foreground="{StaticResource PhoneForegroundBrush}"
                    VerticalAlignment="Bottom"/>
                    <Border.Resources>
                    <valueConverters:TrackGroupToGroupItemBackgroundBrushConverter x:Key="groupItemBackgroundBrush" />
                    </Border.Resources>
                    </Border>
                    </DataTemplate>

                    </phone:PhoneApplicationPage.Resources>
                    

                    And not with this one too:

                    phone:PhoneApplicationPage.Resources
                    <DataTemplate x:Key="groupItemTemplate"
                    <Border Width="99" Height="99" Background="{Binding Path=., Converter={StaticResource groupItemBackgroundBrush}}" Margin="6" IsHitTestVisible="{Binding HasItems}">
                    <TextBlock Text="{Binding GroupTitle}"
                    FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                    FontSize="36"
                    Margin="{StaticResource PhoneTouchTargetOverhang}"
                    Foreground="{StaticResource PhoneForegroundBrush}"
                    VerticalAlignment="Bottom"/>
                    </Border>
                    </DataTemplate>

                    <valueConverters:TrackGroupToGroupItemBackgroundBrushConverter x:Key="groupItemBackgroundBrush" />

                    </phone:PhoneApplicationPage.Resources>
                    
                    1 Reply Last reply
                    0
                    • _ _Madmatt

                      The DataTemplate as well as the Converter are in the

                      <DataTemplate x:Key="groupItemTemplate">
                              <Border Width="99" Height="99" Background="{Binding Converter={StaticResource groupItemBackgroundBrush}}" Margin="6" IsHitTestVisible="{Binding HasItems}">
                                  <TextBlock Text="{Binding GroupTitle}" 
                                                         FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                                                         FontSize="36"
                                                         Margin="{StaticResource PhoneTouchTargetOverhang}"
                                                         Foreground="{StaticResource PhoneForegroundBrush}"                                        
                                                         VerticalAlignment="Bottom"/>
                              </Border>
                          </DataTemplate>
                          
                          <valueConverters:TrackGroupToGroupItemBackgroundBrushConverter x:Key="groupItemBackgroundBrush" />
                      </phone:PhoneApplicationPage.Resources>
                      

                      Is there something wrong in here?

                      S Offline
                      S Offline
                      skv_lviv
                      wrote on last edited by
                      #10

                      Not sure, maybe I'm wrong, but have you tried to put converter definition before your datatemplate?

                      _ 1 Reply Last reply
                      0
                      • S skv_lviv

                        Not sure, maybe I'm wrong, but have you tried to put converter definition before your datatemplate?

                        _ Offline
                        _ Offline
                        _Madmatt
                        wrote on last edited by
                        #11

                        Thanks! This did the trick! Too bad I didn't think of that myself... :D

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups