views:

64

answers:

1

Hi, I have a textbox that has the following simple XAML (not necessary to read it - just have it for reference):

<TextBox Name="m_ctrlUserDeviceType" Style="{StaticResource textStyleTextBox}" Text="{Binding Source={x:Static api:MySettings.Instance}, Path=UserDeviceType, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Validation.Error="TextBox_Error" MinHeight="25" Margin="4" VerticalAlignment="Top" MaxLength="23" VerticalContentAlignment="Center" HorizontalAlignment="Left" MinWidth="100"></TextBox>

For completeness, the style textStyleTextBox looks like this (again, not necessary to read to answer question):

<Style x:Key="textStyleTextBox" TargetType="TextBox">
    <Setter Property="Foreground" Value="#333333" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="MinHeight" Value="2" />
        <Setter Property="MinWidth" Value="100" />
        <Setter Property="Margin" Value="4" />
        <Setter Property="MaxLength" Value="23" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Left" />
       <!-- <Setter Property="Binding Source" Value="{x:Static api:MySettings.Instance}"/>
        <Setter Property="Binding ValidatesOnExceptions" Value="true" />
        <Setter Property="Binding NotifyOnValidationError" Value="true" /> -->



  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
                    Path=(Validation.Errors)[0].ErrorContent}" />
    </Trigger>
  </Style.Triggers>
</Style>

I have a lot of the stuff (MiHeight, Margin, etc.)in the style because I have a lot of these textboxes and they're almost exactly the same. In fact, there's a lot more in common than just the style. The details of the binding to the class MySettings are almost the same. The only difference is which particular property the textbox is binding too. Additionally, I always user TextBox_Error for Validation.Error.

Is there a way to put the binding info in Style or Data Template so I don't have to keep typing it for each textbox?

I would need to be able to assign an individual property (Path) for each textbox, and I suppose I still need the ability to not use any of it for some particular textbox added in the future (that has nothing to do with databinding to MySettings).

Is there a way to put the TextBox_Error part inside of style or DataTemplate? Using Setter Property did not seem to work for me.

I keep mentioning Data Template as I think the answer might have something to do with that based on reading Pro Silverlight 2 in C# 2008. However, I wasn't able to figure it out. I also tried adding stuff to "Style" as you can see from the commented out stuff in that section.

Thanks,

Dave

+2  A: 

I dont think that there is a way to do what you are asking. However, I do think that you could go about it a different way.

What I would look into, is creating a custom control that extends TextBox, then create some dependency properties that, when the control is initialised, setup the bindings and error validation.

This way you can use your custom textbox all over your app and control every property, and even style them the same (just change the target type of your style)

HTH

Mark
Thanks. I'll look into this approach.
Dave