3

我有这个列表框:

 <ListBox Name="lstEmployee" 
                 FontSize="32" 
                 SelectionChanged="lstEmployee_SelectionChanged" 
                 Margin="10,60,10,78" 
                 Grid.RowSpan="2"
                 Foreground="#FF3F575D">

它是这样填充的:

 foreach (var contactdata in RootObject_1.results)
                {

                    StackPanel stk = new StackPanel();
                    stk.Name = "stack" + ctr.ToString();
                    stk.Orientation = System.Windows.Controls.Orientation.Vertical;
                    stk.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

                    TextBlock txtBlk = new TextBlock();
                    txtBlk.Name = "txtBlk" + ctr.ToString();
                    txtBlk.Text = contactdata.name;
                    txtBlk.FontSize = 30;
                    txtBlk.FontWeight = System.Windows.FontWeights.Bold;
                    txtBlk.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    txtBlk.TextWrapping = TextWrapping.Wrap;

                    TextBlock txtBlk1 = new TextBlock();
                    txtBlk1.Name = "txtBlk" + ctr.ToString();
                    txtBlk1.Text = contactdata.formatted_address;
                    txtBlk1.FontSize = 22;
                    txtBlk1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    txtBlk1.TextWrapping = TextWrapping.Wrap;

                    stk.Children.Add(txtBlk);  // index 0  
                    stk.Children.Add(txtBlk1);  // index 1  
                    lstEmployee.Items.Add(stk);

                    referenceList.Add(contactdata.reference.ToString());

                }

如何使用图像更改所选项目的背景?("\Image\Taxi\800\cellselect_80​​0")

更新

为了发出 JSON 请求,我正在使用这个:

void chamaTaxi()
    {

        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        webClient.DownloadStringAsync(new Uri("https://maps.googleapis.com/maps/api/place/textsearch/json?&query=taxi&location=-19.94549444,-43.92314218&&radius=5000&sensor=true&key=AIzaSyDucC8QBV5wu4V-dQXFfABXGaaUzdmT5xw"));

    }
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
            Deployment.Current.Dispatcher.BeginInvoke(()=>
         {

                var RootObject_1 = JsonConvert.DeserializeObject<RootObject_1>(e.Result);

               **code above**
    }
}

对于 JSON 类,我使用此链接:JSON URL和此生成器:生成器

4

2 回答 2

1

这是使用 DataTemplate 执行此操作的快速 MVVM 示例

创建一个ObservableCollection来保存您的数据模型并用您的数据填充列表,然后DataTemplate将在屏幕上显示数据。

DataTemplate里面有一个只有当项目被选中时才会出现ImageVisible

xml:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">

    <Grid DataContext="{Binding ElementName=UI}" >
        <ListBox  ItemsSource="{Binding ContractDataList}" SelectedItem="{Binding SelectedItem}">
            <ListBox.Resources>
                <BooleanToVisibilityConverter x:Key="BoolToVisibleConverter" />
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Image Source="Image\Taxi\800\cellselect_800.png" Stretch="Fill" Visibility="{Binding IsSelected, Converter={StaticResource BoolToVisibleConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}"/>
                            <TextBlock Text="{Binding Address}"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

代码:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace WpfApplication8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private ContractModel _selectedItem;
        private ObservableCollection<ContractModel> _contractDataList = new ObservableCollection<ContractModel>();

        public MainWindow()
        {
            InitializeComponent();

            //foreach (var contactdata in RootObject_1.results)
            for (int i = 0; i < 30; i++)
            {
                var model = new ContractModel
                {
                    Name = "name" + i,
                    Address = "address" + i,
                    Reference = i.ToString()
                };
                ContractDataList.Add(model);
            }
        }

        public ObservableCollection<ContractModel> ContractDataList
        {
            get { return _contractDataList; }
            set { _contractDataList = value; }
        }

        public ContractModel SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value; NotifyPropertyChanged("SelectedItem"); }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }
    }


    public class ContractModel : INotifyPropertyChanged
    {
        private string _name;
        private string _address;
        private string _reference;

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

        public string Address
        {
            get { return _address; }
            set { _address = value; NotifyPropertyChanged("Address"); }
        }

        public string Reference
        {
            get { return _reference; }
            set { _reference = value; NotifyPropertyChanged("Reference"); }


        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

结果: 在此处输入图像描述

于 2013-08-14T00:03:42.267 回答
0

我认为您必须修改 ListBoxItem 的控件模板。三个步骤:

1) 找到默认控制模板ListBoxItem--应该在这个目录中找到

%ProgramFiles(x86)%\Windows Kits\8.0\Include\winrt\xaml\design

2)将您的图像插入控制模板(无论您希望它出现在哪里),并最初使用 opacity=0 隐藏它:

<Image x:Name="image" Source="Images/Taxi\800\cellselect_800" Opacity="0" />

3) 在情节提要下添加一个条目VisualState x:Name="Selected"。这将在选择项目时显示图像:

<DoubleAnimation Storyboard.TargetName="image" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
于 2013-08-13T21:27:20.823 回答