2014-02-22 48 views
2

我試圖創建的Windows Phone 8的項目與數據庫(源碼)技術選擇數據和Windows手機綁定值列表框項目8個應用

我曾嘗試以下:

1)從服務器下載&源碼FIE在本地應用

2)我還可以檢索從本地SQLite數據庫中的值保存它,並通過使用來自This Link &其他一些環節過於一些示例代碼在消息框中顯示。

但是,我無法綁定列表框中的值。

這裏是我的代碼:

XAML代碼:

<Grid x:Name="ContentPanel" Grid.RowSpan="2"> 
     <ListBox Name="scheduleListbox" Margin="5,85,5,60" ItemsSource="{Binding}" SelectionChanged="scheduleListbox_SelectionChanged"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
       <Grid Height="250" Width="480"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="20"></RowDefinition> 
         <RowDefinition Height="40"></RowDefinition> 
         <RowDefinition Height="*"></RowDefinition> 
         <RowDefinition Height="40"></RowDefinition> 
        </Grid.RowDefinitions> 
        <TextBlock Name="team1Name" Text="{Binding team1_name}" Grid.Row="0"></TextBlock> 
        <TextBlock Name="team2Name" Text="{Binding team2_name}"></TextBlock> 
        <TextBlock Name="venue" Text="{Binding venue}" ></TextBlock> 
       </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     <toolkit:ListPicker Name="selectTeam" Height="60" ItemsSource="{Binding}" VerticalAlignment="Bottom" Background="Black" FullModeHeader="Select your Team Schedule" Foreground="White" ExpansionMode="FullScreenOnly" Margin="5,0" SelectionChanged="selectTeam_SelectionChanged"> 
      <toolkit:ListPicker.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Name="listpickerStackpannel" > 
         <TextBlock Text="{Binding}" TextAlignment="Center" FontFamily="Times New Roman" FontSize="30"></TextBlock> 
        </StackPanel> 
       </DataTemplate> 
      </toolkit:ListPicker.ItemTemplate> 
      <toolkit:ListPicker.FullModeItemTemplate> 
       <DataTemplate> 
        <StackPanel Name="listpickerStackpannel" Margin="10"> 
         <TextBlock Text="{Binding}" TextAlignment="Center" FontFamily="Times New Roman" FontSize="30"></TextBlock> 
        </StackPanel> 
       </DataTemplate> 
      </toolkit:ListPicker.FullModeItemTemplate> 
     </toolkit:ListPicker> 
    </Grid> 

和我的CS代碼:

 string DB_PATH = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sample.sqlite"); 
    //SQLite connection 
    private SQLiteConnection dbConn; 
    private List<string> _source = new List<string> 
    { 
     "Full Schedule","Afghanistan","Australia","Bangladesh","England","Hong Kong","India","Ireland","Nepal","Netherlands","New Zealand","Pakistan","South Africa","Sri Lanka","UAE","West Indies","Zimbabwe" 
    }; 
    public MainPage1() 
    { 
     InitializeComponent(); 
     selectTeam.ItemsSource = _source; 
    } 

    private void scheduleListbox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     dbConn = new SQLiteConnection(DB_PATH); 
     /// Create the table Task, if it doesn't exist. 
     dbConn.CreateTable<iccworldt20_schedule>(); 
     /// Retrieve the task list from the database. 
     List<iccworldt20_schedule> retrievedTasks = dbConn.Query<iccworldt20_schedule>("select * from iccworldt20_schedule").ToList<iccworldt20_schedule>(); 
     /// Clear the list box that will show all the tasks. 
     scheduleListbox.Items.Clear(); 
     foreach (var t in retrievedTasks) 
     { 
      MessageBox.Show(t.ToString()); 
     } 
    } 
} 
public class iccworldt20_schedule 
{ 
    [PrimaryKey, AutoIncrement] 
    public int match_id { get; set; } 
    public string team1_Name { get; set; } 
    public string team2_Name { get; set; } 
    public string match_no { get; set; } 
    public string group { get; set; } 
    public string venue { get; set; } 
    public string time { get; set; } 
    public string day { get; set; } 

    public override string ToString() 
    { 
     return team1_Name + ":" + team2_Name +venue; 
    } 
} 

請給出一些解決方案,如何從sqlite的DB和綁定檢索值它的值列表框。,

回答

1

我看不到任何代碼,您將數據添加到列表框。如果您從數據庫中提取數據,請將其添加到ItemsSource屬性中。

scheduleListbox.ItemsSource = retrievedTasks; 

我不明白爲什麼你selectTeam ListPicker使用的ItemsSource =「{結合}」時填寫它在構造函數中。爲什麼你使用相同的scheduleListbox。請參閱Binding overview

如果您使用空綁定語法{Binding}。 ListBox繼承父元素的DataContext。如果未指定路徑,則默認爲綁定到整個對象。

1

我得到它,根據「簡Smuda」答覆我發現該溶液。,

我刪除在XAML代碼結合語法(的ItemsSource =「{結合}」)兩者listpicker和列表框和代碼本身添加的ItemsSource,這樣

所以我的XAML代碼:。

<ListBox Name="scheduleListbox" Margin="5,85,5,60" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid Height="100" Width="480" Margin="0,0,0,5" Background="CadetBlue"> 
         <Grid.RowDefinitions> 
         <RowDefinition Height="*"></RowDefinition> 
         <RowDefinition Height="*"></RowDefinition> 
          <RowDefinition Height="*"></RowDefinition> 
         </Grid.RowDefinitions> 
         <TextBlock Text="{Binding team1_Name}" Name="team1Name" Foreground="White"></TextBlock> 
         <TextBlock Grid.Row="1" Text="{Binding team2_Name}" Name="team2Name" Foreground="Red"></TextBlock> 
         <TextBlock Grid.Row="2" Text="{Binding venue}" Name="venue" Foreground="Yellow"></TextBlock> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     <toolkit:ListPicker Name="selectTeam" Height="60" VerticalAlignment="Bottom" Background="Black" FullModeHeader="Select your Team Schedule" Foreground="White" ExpansionMode="FullScreenOnly" Margin="5,0" SelectionChanged="selectTeam_SelectionChanged"> 
      <toolkit:ListPicker.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Name="listpickerStackpannel" > 
         <TextBlock Text="{Binding}" TextAlignment="Center" FontFamily="Times New Roman" FontSize="30"></TextBlock> 
        </StackPanel> 
       </DataTemplate> 
      </toolkit:ListPicker.ItemTemplate> 
      <toolkit:ListPicker.FullModeItemTemplate> 
       <DataTemplate> 
        <StackPanel Name="listpickerStackpannel" Margin="10"> 
         <TextBlock Text="{Binding}" TextAlignment="Center" FontFamily="Times New Roman" FontSize="30"></TextBlock> 
        </StackPanel> 
       </DataTemplate> 
      </toolkit:ListPicker.FullModeItemTemplate> 
     </toolkit:ListPicker> 

和我的CS這樣的代碼:

string country = "Full Schedule"; 
    List<match_schedule> ScheduleList; 
    // the local folder DB path 
    string DB_PATH = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sample.sqlite"); 
    //SQLite connection 
    private SQLiteConnection dbConn; 
    ProgressIndicator _progressIndicator = new ProgressIndicator(); 
    private List<string> _source = new List<string> 
    { 
     "Full Schedule","Afghanistan","Australia","Bangladesh","England","Hong Kong","India","Ireland","Nepal","Netherlands","New Zealand","Pakistan","South Africa","Sri Lanka","UAE","West Indies","Zimbabwe" 
    }; 
    public Schedule() 
    { 
     InitializeComponent(); 
     selectTeam.ItemsSource = _source; 
     Loaded += Schedule_Loaded; 
    } 

    void Schedule_Loaded(object sender, RoutedEventArgs e) 
    { 

    } 

    private void selectTeam_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     country = (sender as ListPicker).SelectedItem.ToString(); 
     dbConn = new SQLiteConnection(DB_PATH); 
     /// Create the table Task, if it doesn't exist. 
     dbConn.CreateTable<match_schedule>(); 

     if (country == "Full Schedule") 
     { 
      ScheduleList = dbConn.Query<match_schedule>("select * from tableName").ToList<match_schedule>(); 
     } 
     else 
     { 
      ScheduleList = dbConn.Query<match_schedule>("select * from tableName where team1_Name=? or team2_Name=?", country).ToList<match_schedule>(); 
     } 
     scheduleListbox.ItemsSource = ScheduleList;   
    } 
} 
public class match_schedule 
{ 
    [PrimaryKey, AutoIncrement] 
    public int match_id { get; set; } 
    public string team1_Name { get; set; } 
    public string team2_Name { get; set; } 
    public string match_no { get; set; } 
    public string group { get; set; } 
    public string venue { get; set; } 
    public string time { get; set; } 
    public string day { get; set; } 
} 

最後我得從我的本地SQLite數據庫中檢索列的值,我結合它的列表框,感謝揚Smuda應對我,感謝堆棧溢出也

地段,

相關問題