我正在開發一個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.
));
}
謝謝你的回答,對不起,如果它是一個愚蠢的問題,但我怎麼可以參考文本塊? – 2014-10-30 04:10:41
希望上面的代碼可以幫助你繼續! – Joseph 2014-10-30 04:27:58
@vivekshahi你可以在你的c#類中聲明你的文本塊爲私有屬性,如「TextBlock MyTextBlock」。 – Joseph 2014-10-30 05:46:00