【问题标题】:How to access items in binded listbox, wp7如何访问绑定列表框中的项目,wp7
【发布时间】:2010-11-24 21:25:06
【问题描述】:

我有 WP7 的 XAML:

<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>

我的 DataContex 设置为我创建的 ViewModel,它从 web 服务获取数据。数据结构为:

  • 机器(有:Vehicles[])
  • -Vehicles(有:名称、Cars[]、Trucks[]、...)----这就是我绑定到 lbMain 的内容
  • --汽车(有:名称、颜色[]、...)----例如,颜色[0]="red"
  • ---颜色值

我也有图片资源,我想放入 imgColor。

我不知道热到:

  1. 设置每个 imgColor 以根据 txtColor 从资源中获取不同的图像,
  2. 如果(例如)txtColor.Text="red",则对 txtCarName 应用粗体。

感谢任何意见和建议。

【问题讨论】:

    标签: c# xaml windows-phone-7


    【解决方案1】:

    创建Converter 以将颜色名称转换为BitmapImage

    例子:

    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();
        }
    }
    

    【讨论】:

    • 那是很棒的旋风,谢谢!这就是我正在寻找的第一个问题的答案。对我的第二个问题有帮助吗?我在尝试访问父 DataContex 并尝试更改旧的 ListBoxItem 时遇到问题。
    • 您必须以相同的方式使用转换器,即将 FontWeight 绑定到颜色,然后在转换器中将 Color 转换为 FontWeight。
    • 好吧,我同意弗朗切斯科的观点。您只需要另一个转换器将颜色名称转换为FontWeight 类型,而不是上面示例中的BitmapImage 类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多