如何选择一个列表是另一个列表的项目c#

时间:2014-04-03 10:16:48

标签: c# xaml windows-phone-8 listpicker

如何只选择List FeatList中的一个列表?一个FeatList项由CurrencyTypes和Date组成。我只需要向CurList添加CurrencyTypes列表,其中Date等于ListCtrl3.SelectedItem。

    namespace PhoneApp1
{
    public class CurrencyOfDate
    {
    public List<CurrencyType> CurrencyTypes;
    public string Date { get; set; }
    public override string ToString()
    {
        return Date;
    }
}
public class CurrencyType
{
    public string Name { get; set; }
    public string Value { get; set; }

    public override string ToString()
    {
        return Name;
    }
}
public partial class MainPage : PhoneApplicationPage
{
    List<CurrencyType> curList = new List<CurrencyType>();
    public event PropertyChangedEventHandler PropertyChanged;

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

    public List<CurrencyType> CurList
    {
        get { return curList; }
        set
        {
            curList = value;
            InvokePropertyChanged("CurList");
        }
    }
    List<CurrencyOfDate> featList = new List<CurrencyOfDate>();
    public List<CurrencyOfDate> FeatList
    {
        get { return featList; }
        set
        {
            featList = value;
            InvokePropertyChanged("FeatList");
        }
    }
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        Dispatcher.BeginInvoke(() =>
        {
            _download_serialized_data("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml");
        });
    }
    private async void _download_serialized_data(string url)
    {
        HttpClient webclient = new HttpClient();

        try
        {
            var downloadedString =
                await
                    webclient.GetStringAsync(
                        new Uri("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml"));
            XElement xmlData = XElement.Parse(downloadedString);
            XNamespace ns = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref";

            List<CurrencyOfDate> list = new List<CurrencyOfDate>();
            foreach (XElement c in xmlData.Elements(ns + "Cube").Elements(ns + "Cube"))
                list.Add(new CurrencyOfDate()
                {
                    Date = c.Attribute("time").Value,
                    CurrencyTypes =
                        (from k in xmlData.Elements(ns + "Cube").Elements(ns + "Cube").Elements(ns + "Cube")
                            select new CurrencyType()
                            {
                                Name = k.Attribute("currency").Value,
                                Value = k.Attribute("rate").Value
                            }).ToList()
                });
            FeatList = list;
            ListCtrl3.ItemsSource = FeatList;
            foreach (var selItem in list.Where(selItem => ListCtrl3.SelectedItem.ToString() == selItem.Date))
            {
                CurList = selItem.CurrencyTypes.ToList();
                FromList.ItemsSource = CurList;
                break;
            }
        }

和xaml:

     <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,511">
        <toolkit:ListPicker ScrollViewer.VerticalScrollBarVisibility="Auto"
                               ItemsSource="{Binding CurList}" 
                                x:Name="FromList"
                                Margin="10,0,240,0">
            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock FontSize="30" 
                               Text="{Binding Name}">
                        </TextBlock>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
        </toolkit:ListPicker>
    </Grid>
    <Grid>
        <toolkit:ListPicker x:Name="ListCtrl3" ItemsSource="{Binding FeatList}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch" Margin="230,0,10,0">
            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <TextBlock TextWrapping="Wrap"  Text="{Binding Date}"/>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
        </toolkit:ListPicker>
    </Grid>

0 个答案:

没有答案