簡體   English   中英

[UWP] [C#]在列表框中的Web視圖中顯示綁定文本

[英][UWP][C#]Display binding text in webview in listbox

我在列表框中有一個Web視圖,以數據庫中數據綁定的形式顯示一些選項(根據數據庫中選項的數量顯示選項的數量)。 我使用webview是因為答案選項存在,其中包含

標簽。

數據庫:

數據庫

XAML:

<ListBox Name="ListOption" Grid.Row="4" xmlns:m="using:KipinATM_Win10.Tryout.Models" SelectionChanged="ListAlternatives_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate x:DataType="m1:DBOPTION">
                    <StackPanel Orientation="Horizontal">
                        <WebView Margin="4" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

碼:

int i = 0;
            while (alternative.Step() == SQLiteResult.ROW)
            {
                Items.Add(new DBOPTION(Convert.ToInt32(alternative[0]), alternative[1].ToString(), int.Parse(alternative[2].ToString()), Convert.ToInt32(alternative[3])));
                if (int.Parse(alternative[2].ToString()) == 1)
                {
                    thisquestioncorrectindex = i;
                }
                i++;
            }

            Binding myBinding = new Binding();
            myBinding.Source = Items;
            ListOption.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);

DBOPTION.cs:

[SQLite.Net.Attributes.PrimaryKey]
        public int _id { get; set; }

        public string LABEL { get; set; }

        public int IS_CORRECT { get; set; }

        public int QUESTION_ID { get; set; }

        public DBOPTION()
        {
        }

        public DBOPTION(int ID, string Label, int IsCorrect, int QuestionID)
        {
            _id = ID;
            LABEL = Label;
            IS_CORRECT = IsCorrect;
            QUESTION_ID = QuestionID;
        }

我無法在webview上顯示答案選項。 如何在列表框中的webview中顯示它?

注意:Web視圖上顯示的文本是數據庫的LABEL列中的文本

首先,我認為您不需要在ListBox DateTemplateTextBlock或其他一些控件中使用WebView控件就可以滿足您的要求。 對於“因為存在包含標簽的答案選項”,您的意思是說Tag屬性的情況,該屬性用於在所有支持數據綁定的FrameworkElement類上提供通用屬性。

如果確實要將文本綁定到WebView ,則需要使用附加屬性,因為WebView沒有要綁定的屬性。 有關如何執行的詳細信息,請參考本文

例如:

<ListBox Name="ListOption" Grid.Row="4" xmlns:m="using:KipinATM_Win10.Tryout.Models"   >
    <ListBox.ItemTemplate>
        <DataTemplate x:DataType="local:DBOPTION">
            <StackPanel Orientation="Horizontal">
                <WebView Margin="4" local:MyProperties.HtmlString="{Binding LABEL}" Height="300" Width="300" Tag="{Binding _id}"/>
                <TextBlock  Text="{Binding LABEL}" Tag="{Binding _id}"></TextBlock>
            </StackPanel> 
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

后面的代碼:

public sealed partial class MainPage : Page
{
    ObservableCollection<DBOPTION> Items;
    public MainPage()
    {
        this.InitializeComponent();
        Items = new ObservableCollection<DBOPTION>()
        {
            new DBOPTION()
            {
               _id=1,LABEL="test1",IS_CORRECT=1,QUESTION_ID=3
            },
             new DBOPTION()
            {
                _id=1,LABEL="test1",IS_CORRECT=1,QUESTION_ID=3
            },
              new DBOPTION()
            {
                 _id=1,LABEL="test1",IS_CORRECT=1,QUESTION_ID=3
            }
        };

        Binding myBinding = new Binding();
        myBinding.Source = Items;
        ListOption.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);
    }

    private void ListAlternatives_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }
}

public class DBOPTION
{
    public int _id { get; set; }
    public string LABEL { get; set; }
    public int IS_CORRECT { get; set; }

    public int QUESTION_ID { get; set; }

 }
class MyProperties
{
    // "HtmlString" attached property for a WebView
    public static readonly DependencyProperty HtmlStringProperty =
       DependencyProperty.RegisterAttached("HtmlString", typeof(string), typeof(MyProperties), new PropertyMetadata("", OnHtmlStringChanged));

    // Getter and Setter
    public static string GetHtmlString(DependencyObject obj) { return (string)obj.GetValue(HtmlStringProperty); }
    public static void SetHtmlString(DependencyObject obj, string value) { obj.SetValue(HtmlStringProperty, value); }

    // Handler for property changes in the DataContext : set the WebView
    private static void OnHtmlStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        WebView wv = d as WebView;
        if (wv != null)
        {
            wv.NavigateToString((string)e.NewValue);
        }
    }
}

暫無
暫無

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

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