【问题标题】:Display database to listbox将数据库显示到列表框
【发布时间】:2019-04-02 04:03:33
【问题描述】:

我有一个列表框,其中包含从数据库中选择的文本答案并包含 html 语言,例如:

等。我想显示它以便不显示 html 语言。我试图在 webview 中显示它,但

等仍然可见。 XAML:

<ListBox Name="ListOption" Grid.Row="1" Margin="10,20,10,0" Height="auto" xmlns:m="using:KipinSchool_Win10.TryoutData.Models" SelectionChanged="ListAlternatives_SelectionChanged" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                    <ListBox.ItemTemplate>
                        <DataTemplate x:DataType="m:DBOption">
                            <StackPanel Orientation="Horizontal">
                                <WebView Margin="10,10,10,10" local:MyProperties.HtmlString="{Binding Option}" MinHeight="40" MaxHeight="300" HorizontalAlignment="Stretch" Tag="{Binding OID}"/>
                                <TextBlock Text="{Binding Option}" Tag="{Binding OID}" FontSize="19"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

代码:

string strA = @"SELECT DISTINCT* FROM DBOption WHERE QID='" + question[0] + "'";
            var alternative = objConn.Prepare(strA);

            ObservableCollection<DBOption> Items = new ObservableCollection<DBOption>();

            int i = 0;
            while (alternative.Step() == SQLiteResult.ROW)
            {
                Items.Add(new DBOption(alternative[0].ToString(), alternative[1].ToString(), alternative[2].ToString(), alternative[3].ToString()));
            }
            Binding myBinding = new Binding();
            myBinding.Source = Items;
            ListOption.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);

如何处理?

【问题讨论】:

  • 是不是表示要在列表框中渲染富文本?
  • @ManojChoudhari 是的
  • @Rose,可以分享一个 html 字符串示例吗?
  • @NicoZhu-MSFT 例如:

    2 x 15 menit

  • 这个案例你查了吗reply.

标签: c# database uwp listbox


【解决方案1】:

我尝试在 web 视图中显示它,但等仍然可见。 XAML:

我可以重现您的问题,问题是您没有为 WebView 设置宽度和高度属性。并且 html 字符串不包含内容高度。所以WebView 将无法正确显示。请修改如下代码。

<ListBox.ItemTemplate>
    <DataTemplate >
        <StackPanel Orientation="Horizontal">
            <WebView Margin="10,10,10,10" 
                     local:MyProperties.HtmlString="{Binding Option}"    
                     Height="400" Width="200"
                     HorizontalAlignment="Stretch" 
                     Tag="{Binding OID}"/>

        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

【讨论】:

  • 我设置了 Minheight 和 Maxheight 以及 Horizo​​ntal Alignment = "stretch"
  • 但是 webview 不会显示带有 Minheight Maxheight 属性的渲染。请尝试设置 height 和 width 属性。
  • 我加了高度,但是

    等还在webview上

  • @Rose 请检查此代码sample
猜你喜欢
  • 2020-08-01
  • 2017-04-25
  • 2017-10-19
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多