2014-10-30 51 views
0

我正在開發一個windos手機應用程序和數據庫是必需的,但我不能將xaml的文本塊調用到c#類來綁定它們。 這裏是我的XAML代碼從Xaml調用文本塊到c#類

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel Grid.Row="0" Margin="12,17,0,28"> 
     <TextBlock Text="Smart Parking" Style="{StaticResource PhoneTextNormalStyle}"/> 
     <TextBlock Text="History" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
    </StackPanel> 
    <!--ContentPanel - place additional content here--> 
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <ListBox x:Name="ListData"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
          <StackPanel Orientation="Horizontal"> 
          <TextBlock x:Name= "DateTxt" Text="{Binding Date}" TextWrapping="Wrap" /> 
          <TextBlock x:Name= "TimeTxt" Text="{Binding Time}" TextWrapping="Wrap" /> 
          <TextBlock x:Name= "ZoneTxt" Text="{Binding Zone}" TextWrapping="Wrap"/> 
          <TextBlock x:Name= "FloorTxt" Text="{Binding Floor}" TextWrapping="Wrap"/> 
          <TextBlock x:Name= "LatTxt" Text="{Binding location_latitude}" TextWrapping="Wrap" /> 
          <TextBlock x:Name= "LongTxt" Text="{Binding location_longitude}" TextWrapping="Wrap" /> 
         </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</Grid> 

我想打電話給班上所有的文本塊下面ADDINFO類,所以我把它們存儲在數據庫中。

public partial class History : PhoneApplicationPage 
{ 





    // string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite"); 
    ObservableCollection<historyTableSQlite> DB_HistoryList = new ObservableCollection<historyTableSQlite>(); 

    public History() 
    { 

     InitializeComponent(); 
     // AddInfo(); 
     this.Loaded += ReadHistoryList_Loaded; 
    } 



    private void ReadHistoryList_Loaded(object sender, RoutedEventArgs e) 
    { 
     ReadAllContactsList dbhistory = new ReadAllContactsList(); 
     DB_HistoryList = dbhistory.GetAllHistory();//Get all DB contacts 
     ListData.ItemsSource = DB_HistoryList.OrderByDescending(i => i.Id).ToList();//Latest contact ID can Display first 

    } 


    public void AddInfo(object sender, RoutedEventArgs e) 
    { 

     DbHelper Db_helper = new DbHelper(); 
     Db_helper.Insert(new historyTableSQlite(
     //I want to call all the textblock here Is there anyway to do it. 
     )); 

    } 

回答

0

由於您的文本塊位於DataTemplate中,因此無法在文件後面的代碼中直接訪問它們。

你可以做什麼意味着,只需連接文本塊的Loaded事件並保留文本塊的引用,然後您將獲得訪問權限。

您的數據模板將這個樣子

    <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock Loaded="DateTxt_Loaded" x:Name= "DateTxt" Text="{Binding Date}" TextWrapping="Wrap" /> 
          <TextBlock Loaded="TimeTxt_Loaded" x:Name= "TimeTxt" Text="{Binding Time}" TextWrapping="Wrap" /> 
          <TextBlock Loaded="ZoneTxt_Loaded" x:Name= "ZoneTxt" Text="{Binding Zone}" TextWrapping="Wrap"/> 
          <TextBlock Loaded="FloorTxt_Loaded" x:Name= "FloorTxt" Text="{Binding Floor}" TextWrapping="Wrap"/> 
          <TextBlock Loaded="LatTxt_Loaded" x:Name= "LatTxt" Text="{Binding location_latitude}" TextWrapping="Wrap" /> 
          <TextBlock Loaded="LongTxt_Loaded" x:Name= "LongTxt" Text="{Binding location_longitude}" TextWrapping="Wrap" /> 
         </StackPanel> 
        </DataTemplate> 

,並在後面的C#類,你的代碼看起來像

TextBlock DateTxtBlock; 
    TextBlock TimeTxtBlock; 
    TextBlock ZoneTxtBlock; 
    TextBlock FloorTxtBlock; 
    TextBlock LatTxtBlock; 
    TextBlock LongTxtBlock; 

    private void DateTxt_Loaded(object sender, RoutedEventArgs e) 
    { 
     DateTxtBlock = sender as TextBlock; 
    } 

    private void TimeTxt_Loaded(object sender, RoutedEventArgs e) 
    { 
     TimeTxtBlock = sender as TextBlock; 
    } 

    private void ZoneTxt_Loaded(object sender, RoutedEventArgs e) 
    { 
     ZoneTxtBlock = sender as TextBlock; 
    } 

    private void FloorTxt_Loaded(object sender, RoutedEventArgs e) 
    { 
     FloorTxtBlock = sender as TextBlock; 
    } 

    private void LatTxt_Loaded(object sender, RoutedEventArgs e) 
    { 
     LatTxtBlock = sender as TextBlock; 
    } 

    private void LongTxt_Loaded(object sender, RoutedEventArgs e) 
    { 
     LongTxtBlock = sender as TextBlock; 
    } 

然後你就可以訪問所有的TextBlocks在ADDINFO()method.for例如如果你想獲得DateTxtBlock的值,比如DateTxtBlock.Text。

+0

謝謝你的回答,對不起,如果它是一個愚蠢的問題,但我怎麼可以參考文本塊? – 2014-10-30 04:10:41

+0

希望上面的代碼可以幫助你繼續! – Joseph 2014-10-30 04:27:58

+0

@vivekshahi你可以在你的c#類中聲明你的文本塊爲私有屬性,如「TextBlock MyTextBlock」。 – Joseph 2014-10-30 05:46:00

0

它在一個ListBox中。一個ListBox將有一個項目的集合。

我創建了一個模型類,以匹配你綁定到值...

public class PhoneModel 
{ 
    public string Date { get; set; } 
    public string Time { get; set; } 
    public string Zone { get; set; } 
    public string Floor { get; set; } 
    public string location_latitude { get; set; } 
    public string location_longitude { get; set; } 
} 

我已經添加項目到ListBox ...

ListData.Items.Add(new PhoneModel 
{ 
    Date = DateTime.Now.ToShortDateString(), 
    Time = DateTime.Now.ToShortTimeString(), 
    Zone = "PST", 
    Floor = "10th Floor", 
    location_latitude = "35.45112", 
    location_longitude = "-115.42622" 
}); 

ListData.Items.Add(new PhoneModel 
{ 
    Date = DateTime.Now.ToShortDateString(), 
    Time = DateTime.Now.ToShortTimeString(), 
    Zone = "CST", 
    Floor = "11th Floor", 
    location_latitude = "32.45112", 
    location_longitude = "-105.42622" 
}); 

ListData.Items.Add(new PhoneModel 
{ 
    Date = DateTime.Now.ToShortDateString(), 
    Time = DateTime.Now.ToShortTimeString(), 
    Zone = "EST", 
    Floor = "12th Floor", 
    location_latitude = "34.45112", 
    location_longitude = "-112.42622" 
}); 

現在我會得到對列表框中第一行的引用...

public void AddInfo(object sender, RoutedEventArgs e) 
{ 
    // You need to reference a specific row of your ListBox control. 

    // Make sure there is at least 1 item. 
    if (ListData.Items.Count > 0) 
    { 
     // Get a reference to the first bound object (data that is bound to the text blocks). 
     var item = (PhoneModel)ListData.Items[0]; 

    } 
} 

希望這對我很有幫助。

+0

非常感謝你的這個示例,但我必須在數據庫中添加所有這些值,正如你可以在AddInfo()中看到的那樣嗎? – 2014-10-30 04:58:52