Establezca MaxLength de Windows Phone TextBox en función del campo al que está vinculado en Linq To SQL

I have a lot of fields create by SQLMetal for my WP Linq to SQL database that have a data type of nvarchar(##). How do in XAML use the length of the data type to set the MaxLength of my UI textbox?

I really don't want to have to length fixed in my UI code so I have to remember to change it in two spots if my schema changes.

I am 99% sure it makes no difference if my textbox is built in control or from Telerik.

Linq to SQL field:

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Title", DbType = "NVarChar(50)", UpdateCheck = UpdateCheck.Never)]
public string Title
{
    get
    {
        return this._Title;
    }
    set
    {
        if ((this._Title != value))
        {
            this.OnTitleChanging(value);
            this.SendPropertyChanging();
            this._Title = value;
            this.SendPropertyChanged("Title");
            this.OnTitleChanged();
        }
    }
}

Windows Phone 8 XAML:

<telerikPrimitives:RadTextBox x:Name="titleTextBox" Header="Title" 
                              MaxLength="50"
                              HeaderStyle="{StaticResource HeaderAccentStyle}"
                              Text="{Binding Title, Mode=TwoWay}" Grid.Row ="0"/>

I have looked at few WPF answers but they use Behaviours which don't exists in WP.

preguntado el 27 de noviembre de 13 a las 00:11

1 Respuestas

Not sure if it will work in your situation but you could create a Dictionary of the Property names an the Length values and bind them in the Xaml.

In this example I am just using the DefaultValueAttribute as I dont have the System.Data.Linq.Mapping.ColumnAttribute but the same idea will work

   private Dictionary<string, int> _maxlengthValues;
   public Dictionary<string, int> MaxLengthValues
   {
       get
       {
           if (_maxlengthValues == null)
           {
             // horrible example, but does the job
            _maxlengthValues =  this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
                   .ToDictionary(n => n.Name, p => p.GetCustomAttributes(typeof(DefaultValueAttribute), false))
                   .Where(p => p.Value != null && p.Value.Any() && (p.Value[0] as DefaultValueAttribute).Value != null)
                   .ToDictionary(k => k.Key, v=>(int)(v.Value[0] as DefaultValueAttribute).Value);
           }
           return _maxlengthValues;
       }
   }


  [DefaultValue(20)] // 20 char limit
  public string TestString
  {
      get { return _testString; }
      set { _testString = value; NotifyPropertyChanged("TestString"); }
  }

Xaml:

<TextBox Text="{Binding TestString}" MaxLength="{Binding MaxLengthValues[TestString]}" />

respondido 27 nov., 13:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.