當我將文本輸入到Textbox
中時,它同時更新兩個TextBlocks
。我試圖檢索該值以將其保存到SQL數據庫。我已經暫時設置它在MessageBox中顯示值。WPF無法檢索綁定值MVVM
視圖模型/型號:
private decimal _amount;
public decimal Amount
{
get
{
return _amount;
}
set
{
_amount = value;
OnPropertyChanged("Amount");
}
}
文本框綁定:
<TextBox MaxLength="7" Visibility="{Binding Hide1, Converter={StaticResource BoolToVis},FallbackValue=Visible}" Text="{Binding Amount, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" />
的TextBlocks裝訂:
<TextBlock Foreground="Black" Margin="0 0 0 0" VerticalAlignment="Top" Grid.Column="3" Text="{Binding Path=Amount}"/>
<TextBlock Grid.Column="3" Text="{Binding Amount}" Foreground="Black" Margin="0 0 0 0" VerticalAlignment="Bottom"/>
SaveCommand:
private async void Save()
{
try
{
MessageBox.Show(string.Format("{0}", Amount));
}
catch (DbEntityValidationException ex)
{
foreach (var en in ex.EntityValidationErrors)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}, {1}", en.Entry.Entity.GetType().Name, en.Entry.State) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
foreach (var ve in en.ValidationErrors)
{
exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}, {1}", ve.PropertyName, ve.ErrorMessage) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
}
catch(Exception ex)
{
var exceptionDialog = new MessageDialog
{
Message = { Text = string.Format("{0}", ex) }
};
await DialogHost.Show(exceptionDialog, "RootDialog");
}
}
當我點擊保存,該MessageBox
顯示0
編輯:我只是記得我有ViewModel
連接到兩個UserControls
。我的TabLayout
,它處理TabItems
的內容;和包含保存按鈕和TabControl
的工資單,其將TabLayout
加載到每個TabItem
中。
DataContext的兩個是:
public TabLayout()
{
InitializeComponent();
DataContext = new PayslipModel();
}
public Payroll()
{
InitializeComponent();
DataContext = new PayslipModel();
}
這裏的所有代碼都是同一類的一部分嗎?你確定它都是在同一個類的實例上運行嗎?你嘗試過調試嗎? –
您如何將datacontext設置爲您的ViewModel實例? –
@TaiT的我把它設置在代碼隱藏中,就像這個'DataContext = new PayslipModel();' – bruh1234