SL - 整理 - 输入更变事件(使即时输入内容能被更新到vm)

方法一:这个只在WPF有效,Silverlight无效
<TextBox Grid.Column="1" Height="26" Margin="0,38,19,0" TextWrapping="Wrap" VerticalAlignment="Top">
                <TextBox.Text>
                    <Binding Path="UserName" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <vm:UserNameValidationRule></vm:UserNameValidationRule>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

方法二:Silverlight有效

xmlns

:vmroot="clr-namespace:xxx.xx.ViewModels"

------------------------------------------------------------------------------------------------------------------------

<

TextBox Name="TextBoxSource" AutomationProperties.AutomationId="PA_IssuerViews_IssuerSource_TextBox_TextBoxSource" MaxLength="2000"

vmroot:ContentValidationUtility.ContentValidation="{Binding ElementName=TextBoxSource}"

IsEnabled="{Binding Path=IsEnabledUpdatePanel, Mode=TwoWay}" Margin="300,60,100,0" Height="120" VerticalAlignment="Top" AcceptsReturn="True" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Visible" HorizontalAlignment="Left" Width="500">

<TextBox.Text>

<Binding Path="UpdateObject.Source" Mode="TwoWay">

</Binding>

</TextBox.Text>

</TextBox>

------------------------------------------------------------------------------------------------------------------------

public static class ContentValidationUtility

    {

        /// <summary>

        /// Default button property dependency property

        /// </summary>

        public static readonly DependencyProperty TextBoxProperty

            = DependencyProperty.RegisterAttached("ContentValidation", typeof(TextBox), typeof(ContentValidationUtility),

            new PropertyMetadata(OnContentValidationChanged));




        /// <summary>

        /// Get default button

        /// </summary>

        public static string GetContentValidation(DependencyObject dependencyObject)

        {

            return (string)dependencyObject.GetValue(TextBoxProperty);

        }


        /// <summary>

        /// Set default button

        /// </summary>

        public static void SetContentValidation(DependencyObject dependencyObject, Button value)

        {

            dependencyObject.SetValue(TextBoxProperty, value);

        }



        /// On default button changed

        /// </summary>

        private static void OnContentValidationChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)

        {

            TextBox textbox = sender as TextBox;

            if (textbox != null)

            {

                textbox.TextChanged += new TextChangedEventHandler(textboxTextChanged);

            }

        }


        static void textboxTextChanged(object sender, TextChangedEventArgs e)

        {

            DependencyObject dependency = sender as DependencyObject;

            object textBoxObject = dependency.GetValue(TextBoxProperty);

            if (textBoxObject is TextBox)

            {

                TextBox textBox = textBoxObject as TextBox;

                if (textBox.SelectionStart >= 0)

                {

                    var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);

                    if (bindingExpression != null)

                        bindingExpression.UpdateSource();

                }

            }

        }


    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值