在文本块 wpf 中每 4 个字符后添加一个 space
Add a space after every 4 characters in textblock wpf
我有一个用于向用户显示卡号的卡文本块:
<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 个字符添加一个 space,否则如果它是一个文本框,我可以使用这样的东西:
Insert hyphen automatically after every 4 characters in a TextBox
我怎样才能做到这一点?
尝试以下操作:
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);
对于任何想知道的人,正如 Çöđěxěŕ 所建议的那样,答案应该是这样的:
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>
我有一个用于向用户显示卡号的卡文本块:
<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 个字符添加一个 space,否则如果它是一个文本框,我可以使用这样的东西:
Insert hyphen automatically after every 4 characters in a TextBox
我怎样才能做到这一点?
尝试以下操作:
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);
对于任何想知道的人,正如 Çöđěxěŕ 所建议的那样,答案应该是这样的:
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>