开发者

WPF TextBox control filter + validation

开发者 https://www.devze.com 2023-02-18 14:06 出处:网络
I have TextBox bound to a text property in my window with a validation rule (length is = 4).I needed to add filtering so I took ThomasLebruns WPFDeveloperTools FilteredTextBox.That works but when I us

I have TextBox bound to a text property in my window with a validation rule (length is = 4). I needed to add filtering so I took ThomasLebruns WPFDeveloperTools FilteredTextBox. That works but when I use the control I specify each time the Validation.ErrorTemplate and the Binding.ValidationRules - I want to build those into the FilteredTextBox control so they apply automatically to every FilteredTextBox - how to manage that? Currently I have to:

<Window>
  <Grid>
    <ps:FilteredTextBox x:Name="textboxPanId" Type="Hex" Grid.Column="1" Grid.Row="1" Height="35" Margin="0,2,0,2" MaxLength="4"
                        Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}">
      <TextBox.Text>
        <Binding Path="pan_id" UpdateSourceTrigger="PropertyChanged">
          <Binding.ValidationRules>
            <ps:PanIdValidation />
          </Binding.ValidationRules>
        </Binding>
      </TextBox.Text>
    </ps:FilteredTextBox> 

I would like just:

<ps:FilteredTextBox x:Name="textboxPanId" Type="Hex" Grid.Column="1" Grid.Row开发者_C百科="1" Height="35" Margin="0,2,0,2" MaxLength="4">
  <TextBox.Text>
    <Binding Path="pan_id" UpdateSourceTrigger="PropertyChanged"/>
  </TextBox.Text>

but have the error template and validation rule applied. How to move the setting of the Validation.ErrorTemplate and Binding.ValidationRule into resource dictionary with rest of FilteredTextBox xaml?.

<ResourceDictionary>
  <ControlTemplate x:Key="TextBoxErrorTemplate">
    ...
  </ControlTemplate>


I think if you create a style for FilteredTextBox in your ResourceDictionary you can set these there, but at least the ErrorTemplate for almost sure.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号