【WPF】感叹XAML的强大,感叹微软的伟大
【摘要】 在使用XAML进行页面布局的时候,原以为只有Button才可能绑定Command命令(默认对应Click事件),其实通过微软Blend提供的库,一切都可Command。只要记住下面两个命名空间的对应关系即可xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" == Microsoft.Expression.I...
在使用XAML进行页面布局的时候,原以为只有Button才可能绑定Command命令(默认对应Click事件),其实通过微软Blend提供的库,一切都可Command。
只要记住下面两个命名空间的对应关系即可
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" == Microsoft.Expression.Interactions.dll
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" == System.Windows.Interactivity.dll
第一个是使用来进行条件判断的,第二个是命令触发的。上面两个命名空间也可以使用clr-namespace这样的方式直指DLL。
触发命令还可以带上条件,多么牛的设计。
<TextBox Text="" x:Name="input"></TextBox>
<TextBox Text="" x:Name="input2"></TextBox>
<Button Content="Click2">
<i:Interaction.Triggers>
<!-- And -->
<!--
<i:EventTrigger EventName="Click">
<i:Interaction.Behaviors>
<ei:ConditionBehavior>
<ei:ConditionalExpression>
<ei:ComparisonCondition LeftOperand="{Binding Text, ElementName=input}" Operator="Equal" RightOperand="123" />
<ei:ComparisonCondition LeftOperand="{Binding Text, ElementName=input2}" Operator="Equal" RightOperand="XYZ" />
</ei:ConditionalExpression>
</ei:ConditionBehavior>
</i:Interaction.Behaviors>
<i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="2"></i:InvokeCommandAction>
</i:EventTrigger>
-->
<!-- OR -->
<i:EventTrigger EventName="Click">
<i:Interaction.Behaviors>
<ei:ConditionBehavior>
<ei:ConditionalExpression ForwardChaining="Or">
<ei:ComparisonCondition LeftOperand="{Binding Text, ElementName=input}" Operator="Equal" RightOperand="123" />
<ei:ComparisonCondition LeftOperand="{Binding Text, ElementName=input2}" Operator="Equal" RightOperand="XYZ" />
</ei:ConditionalExpression>
</ei:ConditionBehavior>
</i:Interaction.Behaviors>
<i:InvokeCommandAction Command="{Binding TestCommand}" CommandParameter="2"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>条件还可以实现And和Or的逻辑。在不满足条件的情况下命令不执行。
另一个触发方式是通过数据触发,即当输入完成后即可触发相应的逻辑代码,完成输入检验。
<i:Interaction.Triggers>
<!--<ei:DataTrigger Binding="{Binding ElementName=input,Path=Text}" Comparison="Equal" Value="123">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=input}" PropertyName="ToolTip" Value="我是ToolTip改变了"></ei:ChangePropertyAction>
<ei:CallMethodAction TargetObject="{Binding}" MethodName="Alert"></ei:CallMethodAction>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding ElementName=input2,Path=Text}" Comparison="Equal" Value="abc">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=input}" PropertyName="ToolTip" Value="我是ToolTip改变了"></ei:ChangePropertyAction>
<ei:CallMethodAction TargetObject="{Binding}" MethodName="Alert"></ei:CallMethodAction>
</ei:DataTrigger>
</i:Interaction.Triggers>是不是感觉到了XAML的强大,学习WPF如果不明白这些,那你和做WinForm有什么区别,直接拖拽就,然后编写代码,而根本没有使用XAML为我们提供的方便,又怎么能说自己使用WPF了呢。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)