none
How to Mask Phone Number in ListBox in C#, WPF RRS feed

  • Question

  •  I have  one textBox to add Phone Number to listBox. Listbox value will be SAVED in XML file. for Masking phone Number Im Using Following Code :

           string Phone = this.Textbox_PhoneNumber.Text;
           string MaskedPhone = string.Format("XXXXXXX{0}", Phone.Trim().Substring(7, 3));
           ListBox_PhoneNumber.Items.Add(Country_Code + "-" + MaskedPhone);

      this code masked Xml value too, I need Only ListBox Value should be Masked Xml value should be as it is.

    How can I do it in C# ??

    Thanks,

    Tuesday, January 21, 2020 1:12 PM

All replies

  • Hi Sahana,
    if you want save the original value (Phone) you can use ListBox with converter for displaying:

    XAML:

        <ListBox x:Name="ListBox_PhoneNumber">
          <ListBox.Resources>
            <local:ListBoxConverter x:Key="conv"/>
          </ListBox.Resources>
          <ListBox.ItemTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding Converter={StaticResource conv}}"/>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>

    And code:

    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Input;
    
    namespace WpfApp1
    {
    
    ...
    
            string Phone = this.Textbox_PhoneNumber.Text;
            //string MaskedPhone = string.Format("XXXXXXX{0}", Phone.Trim().Substring(7, 3));
            //ListBox_PhoneNumber.Items.Add(Country_Code + "-" + MaskedPhone);
            ListBox_PhoneNumber.Items.Add(Country_Code + "-" + Phone);
    
    ...
    
      public class ListBoxConverter : IValueConverter
      {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          => string.Format("XXXXXXX{0}", value.ToString().Trim().Substring(7, 3));
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    }
    


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Wednesday, January 22, 2020 8:25 AM
  •   In XAML  ListBoxConverter Showing a Error,  The name "ListBoxConverter" does not exist in the namespace "clr-namespace:iWave_Configuration_Tool.Models". 

    How to write namespace for ListBoxConverter ??

    Wednesday, January 22, 2020 10:09 AM
  • Hi Sahana,
    you must declare and use the right namespace:

    XAML:

    <Window x:Class="WpfApp1.Window88"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            xmlns:converter="clr-namespace:iWave_Configuration_Tool.Models"
    
    …
    
        <ListBox x:Name="ListBox_PhoneNumber">
          <ListBox.Resources>
            <converter:ListBoxConverter x:Key="conv"/>
          </ListBox.Resources>
          <ListBox.ItemTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding Converter={StaticResource conv}}"/>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
    

    and in code:

    namespace iWave_Configuration_Tool.Models
    {
    
    …
    
      public class ListBoxConverter : IValueConverter
      {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          => string.Format("XXXXXXX{0}", value.ToString().Trim().Substring(7, 3));
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    
    …
    
    }


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Wednesday, January 22, 2020 11:54 AM