从XAML中绑定页面后的代码获取或设置INotifyPropertyChanged类的属性值

著名的卡尼斯

我已将组框绑定到INotifyPropertyChanged类。

页面资源

    <Page.Resources>
       <current:UserAccountsStatusHandler x:Key="UserAccounts" />
    </Page.Resources>

组合箱

<GroupBox
                Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2"
                Header="Select Action:"
                Foreground="{DynamicResource DynamicFrmFG}"
                VerticalAlignment="Stretch" HorizontalAlignment="Left"
                Height="50"
                DataContext="{StaticResource ResourceKey=UserAccounts}">

                <StackPanel 
                    Orientation="Horizontal"
                    HorizontalAlignment="Stretch" VerticalAlignment="Center"
                    Height="Auto">
                    <RadioButton
                        Content="Insert" 
                        Foreground="{DynamicResource DynamicFrmFG}" Height="16"
                        IsChecked="{Binding Path=UserAccountAction, Converter={StaticResource enumToBooleanConverter}, 
                                  ConverterParameter={x:Static enums:UserAccountActions.Insert}, UpdateSourceTrigger=PropertyChanged}"
                        Margin="0,0,10,0" 
                        Click="RadioButton_Click" />
                    <RadioButton
                        Content="Update" 
                        Foreground="{DynamicResource DynamicFrmFG}" Height="16"
                        IsChecked="{Binding Path=UserAccountAction, Converter={StaticResource enumToBooleanConverter}, 
                                  ConverterParameter={x:Static enums:UserAccountActions.Update}, UpdateSourceTrigger=PropertyChanged}"
                        Margin="0,0,10,0" 
                        Click="RadioButton_Click" />
                    <RadioButton
                        Content="Delete" 
                        Foreground="{DynamicResource DynamicFrmFG}" Height="16"
                        IsChecked="{Binding Path=UserAccountAction, Converter={StaticResource enumToBooleanConverter}, 
                                  ConverterParameter={x:Static enums:UserAccountActions.Delete}, UpdateSourceTrigger=PropertyChanged}"
                        Margin="0,0,10,0" 
                        Click="RadioButton_Click" />
                </StackPanel>
            </GroupBox>

班级

public class UserAccountsStatusHandler : INotifyPropertyChanged
{
    private UserAccountActions userAccountAction;
    public UserAccountActions UserAccountAction 
    {
        get { return userAccountAction; } 
        set 
        {
            userAccountAction = value;
            IsSaveEnabled = (userAccountAction == UserAccountActions.None) ? false : true;
            OnPropertyChanged("UserAccountAction"); 
        } 
    }

    private bool isSavedEnabled;
    public bool IsSaveEnabled { get { return isSavedEnabled; } set { isSavedEnabled = value; OnPropertyChanged("IsSaveEnabled"); } }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

这一切都按预期工作。单击单选按钮之一,属性更改,并且保存按钮变为启用状态。但是,我试图弄清楚如何从页面后面的代码中的Class中提取这些值,而实际上不必从UI中提取groupbox的DataContext,然后调用该类。

如果我尝试在页面后面的代码中创建该类的新实例,则可以得到默认值。到目前为止,我发现如何获取/设置值的唯一方法是将变量的值设置为等于groupboxes数据上下文,如下所示:

        var test = (UserAccountsStatusHandler)tempGroupBoxName.DataContext;
        test.IsSaveEnabled = false;

我已经读过很多东西,它们说明数据层不必对UI有所了解。所以我不知道该怎么做。任何帮助将不胜感激。

编辑:添加我以前做的事,我认为也是错误的。

private UserAccountsStatusHandler mainStatusHandler;
mainStatusHandler = new UserAccountsStatusHandler();               
base.DataContext = mainStatusHandler;

在这一点上,我可以轻松地调用mainStatusHandler来获得像is这样的东西,IsSavedEnabled并确定用户从中选择了什么动作UserAccountAction我使用的唯一真实原因IsSavedEnabled是在选项卡重新加载页面时禁用按钮。为了确保他们在未首先选择实际操作的情况下不会点击保存按钮,然后才启用保存按钮。然后,他们必须实际单击“保存”按钮以执行后面的代码才能将数据保存到服务器。

约翰逊·沙林格

由于您只是将其用于某种形式的验证,因此您确实需要使用ICommand实现。因此,在这种情况下,请修改视图模型以在INotifyPropertyChanged旁边实现ICommand接口。

public class UserAccountsStatusHandler : INotifyPropertyChanged, ICommand
{
    private UserAccountActions userAccountAction;
    public UserAccountActions UserAccountAction
    {
        get { return userAccountAction; }
        set
        {
            userAccountAction = value;
            IsSaveEnabled = (userAccountAction == UserAccountActions.None) ? false : true;
            OnPropertyChanged("UserAccountAction");

            // Refresh the CanExecute method.
            OnCanExecuteChanged();
        }
    }

    private bool isSavedEnabled;
    public bool IsSaveEnabled { get { return isSavedEnabled; } set { isSavedEnabled = value; OnPropertyChanged("IsSaveEnabled"); } }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public bool CanExecute(object parameter)
    {
        return userAccountAction.SomeActionSelected != null;
    }

    public event EventHandler CanExecuteChanged;
    private void OnCanExecuteChanged()
    {
        if (this.CanExecuteChanged!= null)
            this.CanExecuteChanged(this, new EventArgs());
    }

    public void Execute(object parameter)
    {
        // perform save code.
    }
}

现在,在XAML中,如果组框UserAccountStatusHandler的数据上下文为,则可以使用为组框分配名称,然后将按钮绑定到该名称。

<GroupBox x:Name="UserAccountStatusGroupBox"></GroupBox>

<Button Content="Save"
        Command={Binding ElementName=UserAccountStatusGroupBox, Path=DataContext} />

加载视图后,该按钮将调用CanExecute。如果返回值为false,则该按钮将被自动禁用。如果为true,则启用它。更改视图模型中的属性后,您将调用CanExecuteChanged处理程序,它将重新评估是否可以启用保存按钮。

通常,每个视图/用户控件应具有一个视图模型。在这种情况下,您的视图(窗口/页面)应该以视图模型作为其数据上下文,而不是GroupBox。这是最佳做法。

我建议您阅读与MVVM协同工作的命令模式对于这样的事情,应该根本不存在零代码。它都应包含在视图模型中。视图模型的目的是支持视图。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

绑定被忽略后,在“代码背后”中设置的WPF控件属性

来自分类Dev

使用 INotifyPropertyChanged 的 WPF Xaml 绑定类实例

来自分类Dev

使用类字段在 xaml 中设置值

来自分类Dev

在模板引擎中渲染页面后获取页面源代码?

来自分类Dev

页面加载后,NativeScript无法获取或设置UI属性

来自分类Dev

如何从Xaml中的样式中提取设置者值属性?

来自分类Dev

将代码隐藏的定义属性绑定到XAML中的DataContext属性?

来自分类Dev

在派生类中设置绑定属性不会触发在派生类中更改的属性

来自分类Dev

如何在代码中传递对象并将其绑定到XAML页面?

来自分类Dev

如何将另一个类的属性值设置为 XAML 属性

来自分类Dev

无法在角度分量中设置绑定属性的值

来自分类Dev

在代码隐藏中获取WPF RelativeSource绑定的值

来自分类Dev

如何获取电话任务背后代码中的绑定值?

来自分类Dev

实现INotifyPropertyChanged时的属性获取器和设置器?

来自分类Dev

WPF强制xaml重新获取属性的值,尽管属性的设置器未更改

来自分类Dev

C#类,用于在xaml和代码中可绑定的常量

来自分类Dev

使用反射获取INotifyPropertyChanged通知的属性的实际值?

来自分类Dev

在XAML中绑定静态泛型类

来自分类Dev

XAML中的参考共享类用于绑定

来自分类Dev

如何在XAML中绑定ObservableCollection的属性

来自分类Dev

绑定XAML中ItemsControl之外的属性

来自分类Dev

在XAML中绑定Winform控件属性

来自分类Dev

绑定到XAML中的对象的属性

来自分类Dev

从 Episerver 中的特定页面获取特定属性值

来自分类Dev

将textBlock文本绑定到自定义类xaml C#中的值

来自分类Dev

从代码后面获取转换属性的绑定

来自分类Dev

为类中的属性设置默认值

来自分类Dev

如何在Python中为类的属性设置列表值

来自分类Dev

从javascript设置hiddenfield值,但未在代码中获取它

Related 相关文章

  1. 1

    绑定被忽略后,在“代码背后”中设置的WPF控件属性

  2. 2

    使用 INotifyPropertyChanged 的 WPF Xaml 绑定类实例

  3. 3

    使用类字段在 xaml 中设置值

  4. 4

    在模板引擎中渲染页面后获取页面源代码?

  5. 5

    页面加载后,NativeScript无法获取或设置UI属性

  6. 6

    如何从Xaml中的样式中提取设置者值属性?

  7. 7

    将代码隐藏的定义属性绑定到XAML中的DataContext属性?

  8. 8

    在派生类中设置绑定属性不会触发在派生类中更改的属性

  9. 9

    如何在代码中传递对象并将其绑定到XAML页面?

  10. 10

    如何将另一个类的属性值设置为 XAML 属性

  11. 11

    无法在角度分量中设置绑定属性的值

  12. 12

    在代码隐藏中获取WPF RelativeSource绑定的值

  13. 13

    如何获取电话任务背后代码中的绑定值?

  14. 14

    实现INotifyPropertyChanged时的属性获取器和设置器?

  15. 15

    WPF强制xaml重新获取属性的值,尽管属性的设置器未更改

  16. 16

    C#类,用于在xaml和代码中可绑定的常量

  17. 17

    使用反射获取INotifyPropertyChanged通知的属性的实际值?

  18. 18

    在XAML中绑定静态泛型类

  19. 19

    XAML中的参考共享类用于绑定

  20. 20

    如何在XAML中绑定ObservableCollection的属性

  21. 21

    绑定XAML中ItemsControl之外的属性

  22. 22

    在XAML中绑定Winform控件属性

  23. 23

    绑定到XAML中的对象的属性

  24. 24

    从 Episerver 中的特定页面获取特定属性值

  25. 25

    将textBlock文本绑定到自定义类xaml C#中的值

  26. 26

    从代码后面获取转换属性的绑定

  27. 27

    为类中的属性设置默认值

  28. 28

    如何在Python中为类的属性设置列表值

  29. 29

    从javascript设置hiddenfield值,但未在代码中获取它

热门标签

归档