【问题标题】:[UWP][C#]Display binding text in webview in listbox[UWP][C#]在列表框中的 webview 中显示绑定文本
【发布时间】:2018-10-05 16:20:48
【问题描述】:

我在列表框中有一个webview,以数据绑定的形式显示来自数据库的一些选项(显示的选项数量根据数据库中的选项数量)。我使用 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 上显示答案选项。如何在listbox的webview中显示?

注意: webview上显示的文本是数据库LABEL列中的文本

【问题讨论】:

    标签: c# sqlite webview uwp


    【解决方案1】:

    首先,我认为您不需要在DateTemplate 中的ListBoxTextBlock 或其他一些控件中使用WebView 控件,这样就可以满足您的要求。对于您提到的“因为存在包含标签的答案选项”,如果您指的是Tag 属性的场景,它是为所有支持数据绑定的FrameworkElement 类提供通用属性。

    如果您确实想将文本绑定到WebView,则需要使用attached properties,因为WebView 没有要绑定的属性。具体操作方法请参考this article

    例如:

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

    【讨论】:

    • 我试过了,但是listbox中包含LABEL的webview是空的,就像我圈出来的图片:1drv.ms/u/s!Auqiv8Ukng7U-Uokm5GjrOMmRNwi
    • @Rose 你的绑定有问题。我上传了完整的最小演示进行测试,你可以测试并检查你的代码 sn-p。
    • 我试过你的代码,但我有一个问题:webview 上的文本只显示一个。如何处理?
    • " webview 上的文本只显示一个。"这是什么意思?
    • 显示的数据只有1。也就是索引上的数据只有0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2017-05-15
    • 2018-02-07
    相关资源
    最近更新 更多