我已將我的組框綁定到INotifyPropertyChanged類。從XAML中的頁面綁定中獲取或設置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的DataContext像這樣:
var test = (UserAccountsStatusHandler)tempGroupBoxName.DataContext;
test.IsSaveEnabled = false;
很多事情我已經閱讀狀態下的數據層應永遠不需要知道有關UI的任何信息。所以我不知道如何做到這一點。任何幫助將不勝感激。
編輯:添加我以前做過的事情,我認爲也是錯誤的。
private UserAccountsStatusHandler mainStatusHandler;
mainStatusHandler = new UserAccountsStatusHandler();
base.DataContext = mainStatusHandler;
在這一點上,我可以方便地調用mainStatusHandler得到的東西喜歡的是IsSavedEnabled
並確定用戶從UserAccountAction
選擇什麼樣的行動。我使用IsSavedEnabled
的唯一真正原因是在標籤重新加載頁面時禁用按鈕。要確保他們沒有實際選擇一個動作,然後啓用保存按鈕,就不會點擊保存按鈕。然後他們必須實際點擊保存按鈕來執行後面的代碼以將數據保存到服務器。
根據迄今爲止提供的答案,我可能需要更多地閱讀MVVM。我可能沒有像我想的那樣抓住它。 – famousKaneis
我更新了我的答案,爲您提供了一個更適合您的需求的例子。從你的編輯中,很清楚你只需要刷新MVVM和WPF。你不應該把這種東西放在你的代碼後面,它真的屬於視圖模型。 –