Silverlight 中数据上下文的 UpdateSourceTrigger

发布于 2024-10-18 12:10:16 字数 1988 浏览 5 评论 0原文

你好 我正在开发 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}" />

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

Hi
I am working on a MVVM Silverlight app and i use UpdateSourceTrigger Property of TextBox to update the bindingexpression in runtime as follows:

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

In this case i m doing the bindingexpressions manually for each textbox.. Is there a way by which i can do this bindingexpressions for all the textbox controls inside the grid at a single instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏大泽ㄣ 2024-10-25 12:10:16

如果您想更新网格中的每个 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 事件只是一个示例。
也许递归不是解决问题的最好方法,但它是最简单的方法

If you want to update each TextBox in a Grid, you can use this code:

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

Where KeyDown event is just an example.
Perhaps recursion isn't the best way to solve the problem, but it's the easiest way

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文