【发布时间】:2018-05-31 11:01:31
【问题描述】:
我有几个使用相同绑定和行为的文本框。有没有办法在文本框样式或模板或其他一些创造性的解决方案中设置这些,以便在多个 xaml 控件中具有可重复的绑定和/或行为?
这是我正在做的一个例子,你可以看到行为是相同的,我有很多文本框需要验证命令行为,并且每个控件的文本绑定将是相同的减去转换器参数,但我可以将其绑定到控件名称或标签。
<TextBox x:Name="Control1"
Text="{x:Bind NewItem.Params,Mode=TwoWay,Converter={StaticResource DictionaryConverter},ConverterParameter=Control1,UpdateSourceTrigger=PropertyChanged}"
AllowFocusOnInteraction="True"
Style="{StaticResource FormTextBox}"
Grid.Row="1">
<i:Interaction.Behaviors>
<ic:EventTriggerBehavior EventName="LostFocus">
<ic:InvokeCommandAction Command="{x:Bind NewItem.CommandValidate}"/>
</ic:EventTriggerBehavior>
</i:Interaction.Behaviors>
</TextBox>
<TextBox x:Name="Control2"
Text="{x:Bind NewItem.Params,Mode=TwoWay,Converter={StaticResource DictionaryConverter},ConverterParameter=Control2,UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource FormTextBoxNumber}"
Grid.Row="4">
<i:Interaction.Behaviors>
<ic:EventTriggerBehavior EventName="LostFocus">
<ic:InvokeCommandAction Command="{x:Bind NewItem.CommandValidate}"/>
</ic:EventTriggerBehavior>
</i:Interaction.Behaviors>
</TextBox>
对于那些总是对我的问题感到非常困惑并找出错误的树的人,我会尽力重复一遍:什么是重用 xaml 绑定和行为的简单/创造性方法?在几个控件中是通用的?
【问题讨论】: