点击按钮,删除TextBox里面Text的内容。(触发器和行为)

<StackPanel>
    <TextBox x:Name="tbox">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=clearButton,Path=IsPressed}" Value="True">
                        <Setter Property="Text" Value=""/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
    <Button x:Name="clearButton" HorizontalAlignment="Left" Content="Clear">
        <!--<i:Interaction.Behaviors>
            <local:ClearTextBehavior Target="{Binding ElementName=tbox}"/>
        </i:Interaction.Behaviors>-->
        
    </Button>
</StackPanel>

这段为后台代码

 public partial class MainWindow : Window
 {
     public MainWindow()
     {
         InitializeComponent();
     }
 }

 class ClearTextBehavior:Behavior<Button>
 {
     public TextBox Target
     {
         get { return (TextBox)GetValue(TargetProperty); }
         set { SetValue(TargetProperty, value); }
     }

     public static readonly DependencyProperty TargetProperty =
         DependencyProperty.Register("Target", typeof(TextBox), typeof(ClearTextBehavior), new PropertyMetadata(null));

     protected override void OnAttached()
     {
         AssociatedObject.Click += ButtonClick;
     }

     private void ButtonClick(object sender, RoutedEventArgs e)
     {
         Target?.Clear();
     }

 }

这个是设置在textbox上的滚轮操作

<StackPanel>
    <TextBox x:Name="tbox">
        <i:Interaction.Behaviors>
            <local:MouseWheelBehavior MaxValue="100" MinValue="0"/>
        </i:Interaction.Behaviors>
    </TextBox>
    <Button x:Name="clearButton" HorizontalAlignment="Left" Content="Clear" Click="clearButton_Click">
        <!--<i:Interaction.Behaviors>
            <local:ClearTextBehavior Target="{Binding ElementName=tbox}"/>
        </i:Interaction.Behaviors>-->
    </Button>
</StackPanel>
class MouseWheelBehavior:Behavior<TextBox>
{
    public int MaxValue { get; set;}
    public int MinValue { get; set; }
    public int Scale { get; set; } = 1;

    protected override void OnAttached()
    {
        AssociatedObject.MouseWheel += MouseWheel;
    }

    private void MouseWheel(object sender, MouseWheelEventArgs e)
    {
        var value = int.Parse(AssociatedObject.Text);
        if(e.Delta > 0)
        {
            value += Scale;
        }
        else
        {
            value -= Scale;
        }
        if(value > MaxValue)
            value = MaxValue;
        if(value < MinValue)
            value = MinValue;
        AssociatedObject.Text = value.ToString();
    }
}

使用触发器将窗口进行关闭

<StackPanel>
    <Button x:Name="clearButton" HorizontalAlignment="Left" Content="Close">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <i:CallMethodAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}" MethodName="Close"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
</StackPanel>

将鼠标移动上去就变红

<StackPanel>
    <Button x:Name="clearButton" HorizontalAlignment="Left" Content="Close">
        <i:Interaction.Triggers>
            <i:DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button},Path=IsMouseOver}" Value="True">
                <i:ChangePropertyAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Button}}" PropertyName="Background" Value="Red"/>
            </i:DataTrigger>
        </i:Interaction.Triggers>
    </Button>
</StackPanel>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值