0

由于 WPF 中没有NumericUpDown-Control,因此我使用样式创建了一个。

<Style x:Key="NumericUpDown" TargetType="{x:Type TextBox}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type TextBox}">
        <Grid Width="45" Height="20">
          <TextBox Name="txtBox"
                   TextAlignment="Right"
                   Height="20"
                   Width="30"
                   Margin="0,0,15,0"
                   MaxLength="1"
                   Text="{Binding RelativeSource={RelativeSource TemplatedParent},
                                  Path=Text}"/>
          <Button Name="btnUp"
                  Height="10"
                  Content="▲"
                  Width="15"
                  Margin="30,0,0,10"
                  FontSize="5"/>
          <Button Name="btnDown"
                  Height="10"
                  Content="▼"
                  Width="15"
                  Margin="30,10,0,0"
                  FontSize="5"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

(样式代码在 App.xaml 中)

现在我希望按钮更改Textbox. 但我无法设置他们的OnClick事件。是否可以在后面的代码中设置它,所以我不必为窗口中的每个控件创建样式?提前致谢!

4

0 回答 0