简体   繁体   中英

How to access items in binded listbox, wp7

I have XAML for WP7:

<ListBox x:Name="lbMain" DataContext="{Binding}" ItemsSource="{Binding Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock x:Name="txtName" Text="{Binding name}" />
                    <ListBox x:Name="lbCars" DataContext="{Binding}" ItemsSource="{Binding cars}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel x:Name="spnlCars">
                                    <TextBlock x:Name="txtCarName" Text="{Binding name}" />
                                    <ListBox x:Name="lbCarColor" DataContext="{Binding}" ItemsSource="{Binding color}">
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <TextBlock x:Name="txtColor" Text="{Binding colorValue}"/>
                                                <Image Name="imgColor"/>
                                            </DataTemplate>
                                        </ListBox.ItemTemplate>
                                    </ListBox>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

My DataContex is set to ViewModel I've created, which is getting data from webservice. Data structure is:

  • Machines (has: Vehicles[])
  • -Vehicles (has: name, Cars[], Trucks[],...) ----thats what I'm binding to lbMain
  • --Cars (has: name, color[],...) ---- for example, color[0]="red"
  • ---colorValue

I also have images resources which I want to put in imgColor.

I don't know hot to:

  1. set each imgColor to get different image from resources depending on the txtColor,
  2. apply bold font to txtCarName if (for example) txtColor.Text="red".

I appreciate any advice and any suggestion.

Create a Converter to convert color name into a BitmapImage .

Example :

class ColorToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        String colorName = (String)value;

        switch (colorName.ToLower())
        {
            case "red":
                return new BitmapImage(new Uri("..."));
            default:
                return new BitmapImage(new Uri("..."));
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM