以编程方式自定义WPF按钮样式

时间:2017-07-03 06:34:49

标签: wpf vb.net button styles customization

在WPF项目中,使用以下代码自定义Window_Loaded事件中的按钮:

Dim style As Style = New Style
style.TargetType = GetType(Button)
Dim trigger As DataTrigger = New DataTrigger
trigger.Value = "OK"

'set binding
Dim bi As New Binding
Dim pp As New PropertyPath("Content")
bi.Path = pp
bi.RelativeSource = RelativeSource.Self
trigger.Binding = bi ' New Binding(pp.Path)
Dim setter As Setter = New Setter
setter.Property = Button.BackgroundProperty
setter.Value = Brushes.Red
trigger.Setters.Add(setter)

'clear the triggers
style.Triggers.Clear()
style.Triggers.Add(trigger)
btn_Step.Style = style

它编译好了,但按钮没有明显的变化。如何应用此功能?

1 个答案:

答案 0 :(得分:0)

我想出了如何用代码完成 - 这是我的最终解决方案:

$(function(){
  $('form.cart-num button').on('click',function(){
    var $this = $(this),
        $quantity = $this.closest('form').find('input[name=quantity]'),
        $addToCart = $('a.ajax_add_to_cart');
        newQuantity = parseInt( $quantity.val() ) + ( $this.hasClass('quantity-left-minus') ? -1 : 1 );
    if( newQuantity < 1 ){
      newQuantity = 1;
    }
    $quantity.val( newQuantity );
    $addToCart.attr('data-quantity', newQuantity )
  });
});