【问题标题】:UpdateSourceTrigger for Datacontext in SilverlightSilverlight 中 Datacontext 的 UpdateSourceTrigger
【发布时间】:2011-02-21 09:32:29
【问题描述】:

嗨 我正在开发一个 MVVM Silverlight 应用程序,我使用 TextBox 的 UpdateSourceTrigger 属性在运行时更新绑定表达式,如下所示:

.xaml.cs:

BindingExpression beID = txtEmpID.GetBindingExpression(TextBox.TextProperty); beID.UpdateSource();

BindingExpression beAge = txtAge.GetBindingExpression(TextBox.TextProperty); beAge.UpdateSource();

.xaml:

<Grid x:Name="LayoutRoot"
      Background="White" 
      DataContext="{Binding Source={StaticResource keyEMPVM},    
      UpdateSourceTrigger=Explicit}">

    //<Grid.RowDefinitions>

    //<Grid.ColumnDefinitions>

    <sdk:Label Grid.Row="2"
               Grid.Column="1"
               Target="{Binding ElementName=txtEmpID}" />
    <TextBox x:Name="txtEmpID"
             Grid.Row="2"
             Grid.Column="2"
             Style="{StaticResource ContentTextBoxStyle}"
             Text="{Binding emp.ID, Mode=TwoWay, ValidatesOnExceptions=True,
                                    ValidatesOnDataErrors=True,  NotifyOnValidationError=True, UpdateSourceTrigger=Explicit}" />

    <sdk:Label Grid.Row="4"
               Grid.Column="1"
               Target="{Binding ElementName=txtAge}" />
    <TextBox x:Name="txtAge"
             Grid.Row="4"
             Grid.Column="2"
             Style="{StaticResource ContentTextBoxStyle}"
             Text="{Binding emp.Age, Mode=TwoWay, ValidatesOnExceptions=True,
                                    ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=Explicit}" />

    <Button x:Name="btnSubmit"
            Grid.Row="6"
            Grid.Column="1"
            Grid.ColumnSpan="2"
            Style="{StaticResource ContentButtonStyle}"
            Content="Submit"
            Command="{Binding Path=UpdateEmployee}"
            CommandParameter="{Binding emp.ID}" />

在这种情况下,我正在为每个文本框手动执行绑定表达式。有没有一种方法可以在单个实例中为网格内的所有文本框控件执行此绑定表达式。

【问题讨论】:

    标签: silverlight updatesourcetrigger


    【解决方案1】:

    如果您想在网格中更新 每个 TextBox,可以使用以下代码:

        private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
        {
            var grid = (Grid)sender;
            foreach (var tb in GetChildren<TextBox>(grid))
            {
                var be = tb.GetBindingExpression(TextBox.TextProperty);
                if(be != null) be.UpdateSource();
            }
        }
    
        public IEnumerable<T> GetChildren<T>(DependencyObject d) where T:DependencyObject
        {
            var count = VisualTreeHelper.GetChildrenCount(d);
            for(int i = 0; i < count; i++)
            {
                var c = VisualTreeHelper.GetChild(d, i);
                if (c is T)
                    yield return (T)c;
    
                foreach (var c1 in GetChildren<T>(c))
                    yield return c1;
            }
        }
    

    KeyDown 事件只是一个例子。 也许递归不是解决问题的最好方法,但它是最简单的方法

    【讨论】:

      猜你喜欢
      • 2011-09-03
      • 2011-08-28
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 2011-02-17
      • 2010-11-24
      相关资源
      最近更新 更多