重复使用 xaml 多个控件 (UWP) 中常见的绑定和行为

Re-use xaml bindings and behaviors that are common among several controls (UWP)

我有几个使用相同绑定和行为的文本框。有没有办法在文本框样式或模板或其他一些创造性的解决方案中设置这些,以便在多个 xaml 控件中具有可重复的绑定 and/or 行为?

这是我正在做的一个例子,你可以看到行为是相同的,我有很多文本框需要验证命令行为,并且文本绑定对于每个控件都是相同的减去转换器参数,但我可以将其绑定到控件名称或标记。

<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>

对于那些总是对我的问题感到非常困惑并且树错了树的人,我会尽我所能重新迭代:什么是 simple/creative 重用 xaml 几个控件之间通用的绑定和行为?

根据您的要求,您可以使用 UserControl 封装 TextBox 以阻止更多详细信息。为了便于测试,我简化了您的代码。这是我的 UserControl,它只包含一个 TextBox.

<UserControl
    x:Class="BindTest.MyUseerControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:BindTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:ic="using:Microsoft.Xaml.Interactions.Core"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <TextBox x:Name="MyTextBox" Text="{x:Bind Source.Params,Mode=TwoWay}"  AllowFocusOnInteraction="True"  Height="44">
        <i:Interaction.Behaviors>
            <ic:EventTriggerBehavior EventName="LostFocus">
                <ic:InvokeCommandAction Command="{x:Bind Source.CommandValidate}"/>
            </ic:EventTriggerBehavior>
        </i:Interaction.Behaviors>
    </TextBox>
</UserControl>

然后我声明了 Source DependencyProperty 用来存储数据源。

public Info Source
{   
    get { return (Info)GetValue(SourceProperty); }
    set { SetValue(SourceProperty, value); }
}

public static readonly DependencyProperty SourceProperty =
    DependencyProperty.Register("Source", typeof(Info), typeof(MyUseerControl), new PropertyMetadata(0));

我还创建了 Info 模型 class。

public class Info
{
    public string Params { get; set; }
    public ICommand CommandValidate { get; set; }

    public Info()
    {
        this.Params = "Hi Nico";
        this.CommandValidate = new RelayCommand(()=> {

            Debug.WriteLine("This Method Invoke");

        });
    }
}

用法

public MainPage()
 {
     this.InitializeComponent();
     Item = new Info();
 }
public Info Item { get; set; }


<local:MyUseerControl Source="{x:Bind Item}"/>