【发布时间】:2014-09-02 15:07:14
【问题描述】:
我已将我的组框绑定到 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));
}
}
这一切都按预期工作。我单击其中一个单选按钮,属性更改,保存按钮变为启用状态。但是,我试图弄清楚如何在页面后面的代码中从类中提取这些值,而实际上不必从 UI 中提取组框的 DataContext,然后调用类。
如果我尝试在页面后面的代码中创建一个类的新实例,我会得到默认值。到目前为止,我发现如何获取/设置值的唯一方法是将变量的值设置为等于 groupboxes 数据上下文,如下所示:
var test = (UserAccountsStatusHandler)tempGroupBoxName.DataContext;
test.IsSaveEnabled = false;
我读过的很多东西都表明数据层不应该对 UI 有任何了解。所以我不知道如何做到这一点。任何帮助将不胜感激。
编辑:添加我以前做的,我认为也是错误的。
private UserAccountsStatusHandler mainStatusHandler;
mainStatusHandler = new UserAccountsStatusHandler();
base.DataContext = mainStatusHandler;
此时我可以很容易地调用 mainStatusHandler 来获取类似IsSavedEnabled 的内容,并确定用户从UserAccountAction 中选择的操作。我使用IsSavedEnabled 的唯一真正原因是在选项卡重新加载页面时禁用按钮。为了确保他们在没有实际选择操作之前不会点击保存按钮,然后启用保存按钮。然后他们必须实际点击“保存”按钮来执行后面的代码以将数据保存到服务器。
【问题讨论】:
-
根据目前提供的答案,我可能需要阅读更多关于 MVVM 的信息。我可能没有像我想的那样掌握它。
-
我为你更新了我的答案,提供了一个更好的例子来满足你的需求。从您的编辑中,很明显您只需要对 MVVM 和 WPF 进行一些复习。你不应该把这种东西放在你的代码隐藏中,它确实属于视图模型。
标签: c# wpf xaml binding datacontext