title: How to assign a datatemplate to textbox wpf
body: <p>TextBox is supposed to show masked dollar amount for certain access privileges. I created a converter class(inheriting from IValueConverter)  to handle masking by implementing the convert method.</p>
  
  <pre><code>public object Convert(object value, Type targetType, object parameter, 
                    CultureInfo culture)
  </code></pre>
  
  <p>The third parameter is passed true if masking is needed else false.</p>
  
  <p>Called like this:</p>
  
  <pre><code>CurrencyCOnverter converter = new CurrencyConverter();
  
  this._textbox1.Text = converter.Convert(Amount, typeof(string), !this.IsSuperUser,
                            CurrentCulture).ToString();
  </code></pre>
  
  <p>I have about 12 textboxes on UI. Instead of doing this at 12 places, I defined DataTemplates in Resource dictionary which looks like this:</p>
  
  <pre><code>&lt;DataTemplate x:Key="MaskNormalBackgroundTbx"&gt;
  
   &lt;TextBlock TextAlignment="Right" VerticalAlignment="Center"
              TextWrapping="WrapWithOverflow" 
              Text="{Binding "Amount" 
                     Converter={StaticResource CurrencyDisplayConverter}, 
                     ConverterParameter=true}" /&gt;    
  &lt;/DataTemplate&gt;
  
   &lt;DataTemplate x:Key="NoMaskNormalBackgroundTbx"&gt;
  
   &lt;TextBlock TextAlignment="Right" VerticalAlignment="Center" 
              TextWrapping="WrapWithOverflow" 
              Text="{Binding "Amount" 
                     Converter={StaticResource CurrencyDisplayConverter}, 
                     ConverterParameter=false}" /&gt;    
   &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>My Question: Is there a way I can assign this template to textbox by creating a custom textbox just like we assign data templates for ListBox?</p>
  
  <p>Thanks,</p>
  
  <p>Megan.</p>