【问题标题】:Re-use xaml bindings and behaviors that are common among several controls (UWP)重用多个控件 (UWP) 中常见的 xaml 绑定和行为
【发布时间】: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 绑定和行为的简单/创造性方法?在几个控件中是通用的?

【问题讨论】:

    标签: c# binding uwp-xaml


    【解决方案1】:

    根据您的要求,您可以使用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 模型类。

    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}"/>
    

    【讨论】:

    • 是的,用户控件也是我唯一想到的。我希望有一种方法可以在模板或样式中进行绑定,但我认为你的方式是最好的方式。我实际上在我的 ViewModel 中使用了一个字典,所以我不必为每个 TextBox 使用一个接口,ConverterParamter 正在传递字典键,这样我只需要一个接口实例。除此之外,我所做的几乎就像你展示的那样,这让我感觉更好。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2017-02-18
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多