WPF 无法检索绑定值 MVVM [英] WPF Unable to retrieve binding values MVVM

查看:64
本文介绍了WPF 无法检索绑定值 MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Textbox 中输入文本时,它会同时更新两个 TextBlocks.我正在尝试检索该值以将其保存到 sql 数据库中.我暂时将其设置为在 MessageBox 中显示值.

When I enter text into a Textbox, it updates two TextBlocks at the same time. I'm trying to retrieve that value to save it to a sql database. I've temporarily set it up to display the value in a MessageBox.

视图模型/模型:

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"/>

保存命令:

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.

When I hit save, the MessageBox displays 0.

我刚刚记得我将 ViewModel 连接到两个 UserControls.我的TabLayout,处理TabItems的内容;和工资单,其中包含保存按钮和一个 TabControl,它将 TabLayout 加载到每个 TabItem 中.

I've just remembered that I have the ViewModel connected to two UserControls. My TabLayout, which handles the content of TabItems; and Payroll which contains the save button and a TabControl which loads TabLayout into each TabItem.

两者的数据上下文是:

public TabLayout()
    {
        InitializeComponent();
        DataContext = new PayslipModel();
    }

public Payroll()
    {
        InitializeComponent();
        DataContext = new PayslipModel();
    }

推荐答案

如果您希望两个控件共享一个 DataContext,那么您可以从复合根目录注入一个实例,即 App.Xaml.cs 以下内容可能与您的应用程序的布局方式不符,但应该可以为您提供思路.接下来,您可以了解依赖注入以及如何构建对象图,但这是一个开始.

If you want both controls to share a DataContext then you can inject an instance from your composition root, i.e. App.Xaml.cs The following probably doesn't match the way your application is laid out but should give you the idea. Down the road you can look at dependency injection and how to compose your object graph but this a start.

App.xaml

<Application x:Class="App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                 
             Startup="OnAppStartup">
    <Application.Resources>

    </Application.Resources>
</Application>

App.xmal.cs

public partial class App : Application {

    private void OnAppStartup(object sender, StartupEventArgs e) {                        
        Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
        var vm = new PayslipModel();
        var mainWindow = new MainWindow(vm);
        Application.Current.MainWindow = mainWindow;
        mainWindow.Show();
    }
}

MainWindow.xaml.cs

public MainWindow(PayslipModel vm){
    InitializeComponent();
    tabControl = new TabControl(vm);
    payRoll = new PayRoll(vm);
}

用户控件

public TabLayout(PayslipModel vm)
{
    InitializeComponent();
    DataContext = vm;
}

public Payroll(PayslipModel vm)
{
    InitializeComponent();
    DataContext = vm;
}

这篇关于WPF 无法检索绑定值 MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆