粗体文本或不带字体样式的文本

粗体文本或不带字体样式的文本

本文介绍了以编程方式分配TextBlock(粗体文本或不带字体样式的文本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一个简单的应用程序,它通过来自Web服务器的JSON请求读取订单商品列表,并将其显示在屏幕上.

I have a simple application where it read a list of order items via JSON request from a webserver and displays it on the screen.

现在我想根据分配的文本值将列表中的某些文本样式设置为粗体...

Now I want to set certain text style in the list to bold depending on the assigned text value...

尽管到目前为止,这种构造还算不上运气:

Though had no luck with this construct so far:

        private async void GetOrderItems(string url)
        {
            var response = await httpClient.GetAsync(url);
            if (response.IsSuccessStatusCode)
            {
                var orders = await response.Content.ReadAsStringAsync();
                var emp = JsonArray.Parse(orders);
                var qry = from m in emp

                          select new OrderItemsClass()
                          {
                              productID = m.GetObject()["productid"].GetString(),
                              productPieces = Int32.Parse(m.GetObject()["quantity"].GetString()) > 1 ? "<Bold>" + m.GetObject()["quantity"].GetString() + "</Bold>" : m.GetObject()["quantity"].GetString(),
                              productPrice = "CHF " + m.GetObject()["price"].GetString(),
                              productName = m.GetObject()["name_de"].GetString() + "\n" + m.GetObject()["description_de"].GetString(),
                              productImage = "http://www.example.com/image/product/" + m.GetObject()["filename"].GetString() + "_200.jpg",
                          };
                orderItems.DataContext = qry;
            }
        }

推荐答案

我看到您正在尝试将XAML标记连接到其中一个字段的文本中,然后对文本进行数据绑定.这可以在Web应用程序中使用,但是在XAML中不起作用,因为XAML会被解析并转换为以下位置的对象:

I see that you are attempting to concatenate the XAML tags into the text of one of the fields, and then databinding the text. This would work in a web application, but it doesn´t work in XAML, because the XAML is parsed and converted into objects at the time that the form is loaded, not at the time that the databinding is performed.

要根据数量更改productPieces的样式,我建议在XAML一侧的样式上使用DataTrigger.您根本不会在C#端执行任何操作,只需将数量分配给变量即可.

To change the style of the productPieces depending on the quantity, I'd recommend a DataTrigger on the Style on the XAML side. You would not do anything at all on the C# side, just assign the quantity to the variable.

如果您需要编写DataTrigger的帮助,我建议在WPF论坛中进行询问,因为无需对C#代码进行任何操作.

If you need help writing the DataTrigger, I suggest asking in the WPF forum, since nothing has to be done on the C# code.


这篇关于以编程方式分配TextBlock(粗体文本或不带字体样式的文本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:30