开发者

Windows Phone 7 - Images & Themes in a databound listbox

I have a listbox that I'm doing some databinding. As you can see I have some images associated with the data that I'm displaying... Everything is working great, except that when I change themes I need to change my image to the black images. I can't seem to figure out how to change the images when they 开发者_开发百科are bound like this.

Any ideas?

            <ListBox x:Name="lbPharm" ItemsSource="{Binding col}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
                            <TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,10">*</TextBlock>
                            <StackPanel>
                                <TextBlock x:Name="ItemText" Text="{Binding name}"  FontSize="{StaticResource PhoneFontSizeLarge}"/>
                                <TextBlock x:Name="ItemNumber" Text="{Binding number}"  FontSize="{StaticResource PhoneFontSizeNormal}"/>
                            </StackPanel>

                            <Image Source="Images/phone.png" Margin="20,0" x:Name="phone" Visibility="Visible">
                                <toolkit:GestureService.GestureListener>
                                    <toolkit:GestureListener Tap="GestureListener_Tap_Phone"/>
                                </toolkit:GestureService.GestureListener>
                            </Image>


                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox> 


You should create a binding for the image source instead of setting it explicitly in XAML.

<ListBox x:Name="lbPharm" ItemsSource="{Binding col}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
                <TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,10">*</TextBlock>
                <StackPanel>
                    <TextBlock x:Name="ItemText" Text="{Binding name}"  FontSize="{StaticResource PhoneFontSizeLarge}"/>
                    <TextBlock x:Name="ItemNumber" Text="{Binding number}"  FontSize="{StaticResource PhoneFontSizeNormal}"/>
                </StackPanel>

                <!-- Image source is bound to a property -->
                <Image Source="{Binding ImageSource}" Margin="20,0" x:Name="phone" Visibility="Visible">
                    <toolkit:GestureService.GestureListener>
                        <toolkit:GestureListener Tap="GestureListener_Tap_Phone"/>
                    </toolkit:GestureService.GestureListener>
                </Image>


            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox> 

Now, just update the property in your view model as needed, as long as the class containing the property implements INotifyPropertyChanged the new image will show up in your ListBox.

The ImageSource property may have to be a BitmapImage instead of a string. XAML must use a converter to convert your path string when it is used as a literal but I think it doesn't do this if you use a binding. Or you could use your own converter. Either way, construct a BitmapImage as follows:

new BitmapImage( new Uri( "/path/to/image.png", UriKind.Relative ) )

EDIT

Adding an example:

<DataTemplate x:Key="LongListSelectorItemTemplate">
  <StackPanel VerticalAlignment="Top"
              Orientation="Horizontal">

    <toolkit:GestureService.GestureListener>
      <toolkit:GestureListener Tap="OnTap" />
    </toolkit:GestureService.GestureListener>

    <Image  Source="{Binding ImageSource}"
            MinHeight="32"
            MinWidth="32"
            MaxHeight="48"
            MaxWidth="48" />

    <StackPanel>

      <TextBlock Text="{Binding Name}"
                 Style="{StaticResource PhoneTextExtraLargeStyle}"
                 Margin="12,10,12,0" />

      <TextBlock Text="{Binding Parent}"
                 Foreground="{StaticResource PhoneAccentBrush}"
                 Style="{StaticResource PhoneTextSubtleStyle}"
                 Margin="24,0,12,10" />

    </StackPanel>

  </StackPanel>
</DataTemplate>

Corresponding view model:

public class Item : INotifyPropertyChanged
{
  #region Private Members
  private string _name = null; 
  private string _imageSource = null;
  private string _parent = null;
  #endregion

  public string Name
  {
    get
    {
      return _name;
    }
    set
    {
      if( value != _name ) {
        _name = value;
        NotifyPropertyChanged( "Name" );
      }
    }
  }

  public string Parent
  {
    get
    {
      return _parent;
    }
    set
    {
      if( value != _parent ) {
        _parent = value;
        NotifyPropertyChanged( "Parent" );
      }
    }
  }

  public string ImageSource
  {
    get
    {
      return _imageSource;
    }
    set
    {
      if( value != _imageSource ) {
        _imageSource = value;
        NotifyPropertyChanged( "ImageSource" );
      }
    }
  }

  #region INotifyPropertyChanged Members
  public event PropertyChangedEventHandler PropertyChanged;
  private void NotifyPropertyChanged( String propertyName )
  {
    PropertyChangedEventHandler handler = PropertyChanged;
    if( null != handler ) {
      handler( this, new PropertyChangedEventArgs( propertyName ) );
    }
  }
  #endregion
}

This is code from a project I'm working on, it is similar to your case except I'm displaying the image with a LongListSelector instead of a ListBox. And it looks like I mislead you earlier about not being able to use a string directly for the image source, I'm doing exactly that and it works. Sorry about that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜