C# 在textblock wpf中每4个字符后添加一个空格

C# 在textblock wpf中每4个字符后添加一个空格,c#,wpf,textblock,C#,Wpf,Textblock,我有一个卡片文本块,用于向用户显示卡号: <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"/> 我这样做是为了在写入文本框后,将其键入文本块: <Te

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

<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"/>

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

ccCard.Text = string.Join(" ", Enumerable.Range(0, txtBox.Text.Length / 4).Select(i => txtBox.Text.Substring(i * 4, 4)));
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();
    }
}

我如何才能做到这一点?

请尝试以下操作:

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

对于任何想知道的人来说,正如乔什所建议的那样,答案是这样的:

ccCard.Text = string.Join(" ", Enumerable.Range(0, txtBox.Text.Length / 4).Select(i => txtBox.Text.Substring(i * 4, 4)));
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();
    }
}

您可以创建如下转换器:

ccCard.Text = string.Join(" ", Enumerable.Range(0, txtBox.Text.Length / 4).Select(i => txtBox.Text.Substring(i * 4, 4)));
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>


请向我们展示您尝试过的内容,以及不起作用的内容,以便我们帮助您解决问题。正如目前所写,没有人试图解决这个问题;完成这项任务的方法有很多种。@đěxěŕ我不知道,这就是我需要的帮助。让我直说吧。您有一个
TextBox
,当用户键入时,您想用它更新
TextBlock
,包括空格正确吗?另外,
我不知道
,您是在谈论如何分离字符串,如何在其他数据更改时更新其他控件等。请明确说明您需要帮助的内容。当用户在文本框中键入文本时,文本然后放入文本块,我想每4个字符添加一个“”,在不分离字符串的情况下,在类
private void txtBox_TextChanged(object sender,textchangedventargs e){ccCard.Text=string.Join(“),Enumerable.Range(0,txtBox.Text.Length/4)。选择(i=>txtBox.Text.Substring(i*4,4));}
。接下来,请确保您有一个名为:
txtBox
的文本框,并且它有一个
TextChanged
的处理程序,比如:
TextChanged=“txtBox\u TextChanged”
。对这里发生的事情以及如何解决用户问题的解释会很有帮助,我没有看到。同样值得一提的是,这个
linq
语句效率低下。一个简单的带有范围和选择的枚举表就可以了…你的和我的哪个更有效。对于一个很长的字符串,您的方法必须从字符串的开头开始不断计数。是否对其进行基准测试?另外,如何解释您的解决方案,以便OP能够理解?