我已将组框绑定到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] 删除。
我来说两句