Question about data template or style in WPF xaml

I have a text box that has the following simple XAML (it is not necessary to read it), just use 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 of the textStyleTextBox looks like this (again, it is not necessary to read the answer to the 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 many things (MiHeight, Margin, etc.) in style, because I have many of these text fields, and they are almost exactly the same. In fact, there is much more to it than just style. The details of binding to the MySettings class are almost the same. The only difference is which specific property the text field has. In addition, I am always a TextBox_Error user for Validation.Error.

Is there a way to place the binding information in a data style or template, so I don’t need to type it for each text field?

(Path) , , , ( MySettings).

TextBox_Error DataTemplate? Setter, , .

Data Template, , , - , Pro Silverlight 2 # 2008. . "", .

,

Dave

+3
1

, , . , , -.

, , TextBox, , .

( )

+4

Source: https://habr.com/ru/post/1768903/


All Articles