【问题标题】:Add a space after every 4 characters in textblock wpf在文本块 wpf 中每 4 个字符后添加一个空格
【发布时间】:2019-12-27 15:41:59
【问题描述】:

我有一个卡片文本块,用于向用户显示卡号:

<TextBlock x:Name="ccCard" Text="0000 0000 0000 0000" HorizontalAlignment="Center" 
Foreground="LightGray" FontFamily="Global Monospace" Grid.ColumnSpan="4" Margin="0,0,0,0.4" Width="200"/>

我已经做到了,当一个文本框被写入时,它将它输入到文本块中:

<TextBlock x:Name="ccCard" Text="0000 0000 0000 0000" HorizontalAlignment="Center" 
Foreground="LightGray" FontFamily="Global Monospace" Grid.ColumnSpan="4" Margin="0,0,0,0.4" 
Width="200"/>

我想让它在 textblock 中每 4 个字符添加一个空格,否则如果它是一个文本框,我可以使用如下内容:

Insert hyphen automatically after every 4 characters in a TextBox

我怎样才能做到这一点?

【问题讨论】:

  • 请向我们展示您尝试过的方法和无效的方法,以便我们为您提供帮助。正如目前所写的那样,没有尝试解决这个问题。有很多方法可以完成这项任务。
  • @Çöđěxěŕ 我不知道,这就是我需要帮助的原因
  • 让我直说。你有一个TextBox,当用户键入时,你想更新一个TextBlock,其中包括一个空格,对吗?还有I have no idea,您是在谈论如何分隔字符串,如何在其他数据更改时更新其他控件等。请明确说明您需要什么帮助。
  • 当用户输入文本框,然后将文本放入文本块中,我想每4个字符添加一个“”,而不分隔字符串
  • 把这个扔到你的课堂上private void txtBox_TextChanged(object sender, TextChangedEventArgs e) { ccCard.Text = string.Join(" ", Enumerable.Range(0, txtBox.Text.Length / 4).Select(i =&gt; txtBox.Text.Substring(i * 4, 4))); }。接下来确保您有一个名为:txtBox 的文本框,并且它有一个用于 TextChanged 的处理程序,例如:TextChanged="txtBox_TextChanged"

标签: c# wpf textblock


【解决方案1】:

对于任何想知道的人,正如 Çöđěxěŕ 所建议的,答案看起来像这样:

ccCard.Text = string.Join(" ", Enumerable.Range(0, txtBox.Text.Length / 4).Select(i => txtBox.Text.Substring(i * 4, 4)));

【讨论】:

    【解决方案2】:

    你可以像这样创建一个转换器:

    public class SeperatorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(string))
                throw new InvalidOperationException("The target must be a string");
    
            if (value != null)
            {
                var res = string.Join(" ",
                    Enumerable.Range(0, value.ToString().Length / 4).Select(i => value.ToString().Substring(i * 4, 4)));
    
                return res;
            }
    
            return string.Empty;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    为了使用它,你应该这样做:

         <Window.Resources>
        <local:SeperatorConverter x:Key="seperatorConverter"/>
    </Window.Resources>
    <StackPanel>
        <TextBox Name="TextBox1" Width="200"/>
        <TextBlock Text="{Binding ElementName=TextBox1,Path=Text,Converter={StaticResource seperatorConverter}}"/></StackPanel>
    

    【讨论】:

      【解决方案3】:

      尝试以下:

                 string input = "0123456789012345678901234567890";
                 string[] split = input.Select((x, i) => new { chr = x, index = i })
                      .GroupBy(x => x.index / 4)
                      .Select(x => string.Join("", x.Select(y => y.chr).ToArray()))
                      .ToArray();
                 string results = string.Join(" ", split);
      

      【讨论】:

      • 解释这里发生的事情以及它将如何解决用户的问题会有所帮助,我没有看到。另外值得一提的是,这个linq 声明效率低下恕我直言。一个带有范围和选择的简单枚举就可以了……
      • 你的和我的哪个效率更高。对于很长的字符串,您的方法必须从字符串的开头开始计数。
      • 基准测试?还要解释一下您的解决方案以便 OP 能够理解吗?
      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      • 2020-08-23
      • 2013-12-13
      • 1970-01-01
      相关资源
      最近更新 更多