[英][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
DateTemplate
, TextBlock
或其他一些控件中使用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.