XAML 将逗号添加到wpf中StringFormat内的字符串

zdwk9cvp  于 2023-02-17  发布在  其他
关注(0)|答案(3)|浏览(217)

我有这个文本块

<TextBlock>
      <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}, {1}, {2}, ">
          <Binding Path="object.strProp1" />
          <Binding Path="object.strProp2" />
          <Binding Path="object.strProp3" />
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>

让我们假设 object 不为空,并且*strProp1* = "strProp1"*strProp2* = "strProp2"*strProp2* = "strProp2"
其输出如下所示:

strProp1, strProp2, strProp3,

我想知道的是,每当 object 为null或某个属性为空时,如何删除",“。也就是说,如果 object 为null,则文本块将为空。或者,如果某个对象为空,则它将为空。
有什么建议吗?谢谢!编辑:最好仅在xaml中:)

nwwlzxa7

nwwlzxa71#

我知道这是一个老问题,但我做了一件类似的事情,并想出了这个解决方案。你只需要转义','就像你用''转义字符串中的特殊字符一样。
所以你的绑定应该是:

<TextBlock>
   <TextBlock.Text>
     <MultiBinding StringFormat="{}{0}\, {1}\, {2}\, ">
       <Binding Path="object.strProp1" />
       <Binding Path="object.strProp2" />
       <Binding Path="object.strProp3" />
     </MultiBinding>
   </TextBlock.Text>
</TextBlock>
tpgth1q7

tpgth1q72#

必须使用Converter
MultiValueConverter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace DataBinding
{
    public class MultiStringConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType,
               object parameter, System.Globalization.CultureInfo culture)
        {
            if (values != null)
            {
                StringBuilder formattedString = new StringBuilder();
                int count = 0;
                foreach (var item in values)
                {
                    if (string.IsNullOrEmpty((String)item) == false)
                    {
                        if (count == 0)
                            formattedString.Append(item);
                        else
                            formattedString.Append(", " + item);
                        count++;
                    }

                }
                return formattedString.ToString();
            }
            else
                return null;

        }
        public object[] ConvertBack(object value, Type[] targetTypes,
               object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("Cannot convert back");
        }
    }
}

XAML语言

<Window x:Class="DataBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataBinding"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:MultiStringConverter x:Key="multiStringConverter"/>
    </Window.Resources>

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource multiStringConverter}">
                <Binding Path="object.strProp1" />
                <Binding Path="object.strProp2" />
                <Binding Path="object.strProp3" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>

</Window>
k3fezbri

k3fezbri3#

<TextBlock TextWrapping="Wrap">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Address.FullAddress}" Value="">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
    <Run Text="{Binding Address.LineOne}"/>
    <Run Text="{Binding 
        Address.LineTwo, 
        StringFormat='{}{0}, ',
        TargetNullValue=''}"/>
    <Run Text="{Binding 
        Address.City, 
        StringFormat='{}{0}, ',
        TargetNullValue=''}"/>
    <Run Text="{Binding Address.StateProvince}"/>
    <Run Text="{Binding 
        Address.Zip, 
        StringFormat='{}{0}, ',
        TargetNullValue=''}"/>
    <Run Text="{Binding Address.Country}"/>
</TextBlock>

相关问题