簡體   English   中英

從Xaml調用Textblock到c#類

[英]Calling Textblock from Xaml to the c# class

我正在開發Windos電話應用程序,並且需要數據庫,但是我無法將文本塊從xaml調用到c#類來綁定它們。 這是我的XAML代碼

 <!--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類的下面的類中調用所有textblock,以便將它們存儲在數據庫中。

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.
        ));

    }

由於您的文本塊位於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;
    }   

然后,您可以訪問AddInfo()方法中的所有文本塊,例如,如果要獲取DateTxtBlock.Text之類的DateTxtBlock值。

它在一個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; }
}

我已將項目添加到列表框...

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"
});

現在,我將獲得對ListBox中第一行的引用。

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];

    }
}

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM