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

时光总嘲笑我的痴心妄想 提交于 2019-12-11 16:37:30

问题


I have a webview in the listbox to display some options in the form of data bindings from the database (the number of options displayed according to the number of options in the database). I use webview because the answer option exists that contains the

tag.

Database:

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>

Code:

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

I have trouble displaying the answer option on webview. How to display it in webview in listbox?

Note: Text displayed on the webview is a text in the LABEL column of the database


回答1:


Firstly I don't think you need to use WebView control inside DateTemplate of ListBox, TextBlock or some other controls could just simply meet your requirements. For "because the answer option exists that contains the tag" you mentioned, if you mean the scenario for the Tag property, which is to provide an general-purpose property on all FrameworkElement classes that supports data binding.

If you do want to binding text to WebView, you need to use attached properties since WebView doesn't have a property to bind to. Details for how to do please reference this article.

For example:

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

Code behind:

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


来源:https://stackoverflow.com/questions/50018047/uwpcdisplay-binding-text-in-webview-in-listbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!