[UWP][C#]在列表框的webview中显示绑定文本

[UWP][C#]在列表框的webview中显示绑定文本,c#,sqlite,webview,uwp,C#,Sqlite,Webview,Uwp,我在列表框中有一个webview,它以数据库中数据绑定的形式显示一些选项(选项的数量根据数据库中选项的数量显示)。我使用webview是因为存在包含标记的应答选项 数据库: XAML: DBOPTION.cs: [SQLite.Net.Attributes.PrimaryKey] public int _id { get; set; } public string LABEL { get; set; } public int IS_CORRE

我在列表框中有一个webview,它以数据库中数据绑定的形式显示一些选项(选项的数量根据数据库中选项的数量显示)。我使用webview是因为存在包含标记的应答选项

数据库:

XAML:

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中显示它

注:
webview上显示的文本是数据库标签列中的文本

首先,我认为您不需要使用
列表框
日期模板
中的控件、
文本块
或其他一些控件即可满足您的要求。对于您提到的“因为存在包含标记的应答选项”,如果您指的是属性的场景,即在所有支持数据绑定的
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);
        }
    }
}
公共密封部分类主页面:第页
{
可观察的收集项目;
公共主页()
{
this.InitializeComponent();
Items=新的ObservableCollection()
{
新DBOPTION()
{
_id=1,LABEL=“test1”,是否正确=1,问题id=3
},
新DBOPTION()
{
_id=1,LABEL=“test1”,是否正确=1,问题id=3
},
新DBOPTION()
{
_id=1,LABEL=“test1”,是否正确=1,问题id=3
}
};
Binding myBinding=新绑定();
myBinding.Source=Items;
ListOption.SetBinding(ItemsControl.ItemsSourceProperty,myBinding);
}
private void ListAlternations\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
}
}
公共类DBOPTION
{
公共int_id{get;set;}
公共字符串标签{get;set;}
公共int是_正确的{get;set;}
公共整数问题_ID{get;set;}
}
类别MyProperties
{
//WebView的“HtmlString”附加属性
公共静态只读DependencyProperty HtmlStringProperty=
DependencyProperty.RegisterAttached(“HtmlString”、typeof(string)、typeof(MyProperties)、new PropertyMetadata(“OnHtmlStringChanged”);
//接二连三
公共静态字符串GetHtmlString(DependencyObject obj){return(string)obj.GetValue(HtmlStringProperty);}
公共静态void SetHtmlString(DependencyObject对象,字符串值){obj.SetValue(HtmlStringProperty,值);}
//DataContext中属性更改的处理程序:设置WebView
HtmlStringChanged上的私有静态无效(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
WebView wv=d作为WebView;
如果(wv!=null)
{
wv.NavigateToString((string)e.NewValue);
}
}
}

我已经尝试过了,但是列表框中包含标签的webview是空的,就像我圈出的图像一样:@Rose这将是您的绑定有问题。我上传了完成的最小测试演示,可以很好地进行测试。您可以测试和检查您的代码片段。我尝试了您的代码,但遇到了一个问题:webview上的文本只显示一个。如何处理?“网络视图上的文本仅显示一个。”这意味着什么?显示的数据仅为1。这是索引上的数据,仅为0
<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);
        }
    }
}