Как привязать данные к событию textchanged с помощью методов WPF и MVVM?
Здравствуйте сэр,
у меня есть 5 текстовых полей ....
4 для ввода входного значения и 5-й для хранения результата.
я использовал событие textchange и те события, которые я реализовал в view (файл. cs)..
но я хочу реализовать то же самое в файле view_model...
как сделать то же самое, используя технику wpf и mvvm..
Что я уже пробовал:
<UserControl x:Class="task2.Views.calc" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:task2.Views" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"></ColumnDefinition> <ColumnDefinition Width="150"></ColumnDefinition> <ColumnDefinition Width="100"></ColumnDefinition> <ColumnDefinition Width="150"></ColumnDefinition> </Grid.ColumnDefinitions> <Label x:Name="lblItem" Content="Materials" Grid.Row="0" Grid.Column="0" VerticalAlignment="Top"></Label> <Label x:Name="lblPrice" Content="Price" Grid.Row="0" Grid.Column="1" VerticalAlignment="Top"></Label> <Label x:Name="lblOil" Content="Oil" Grid.Row="1" Grid.Column="0" VerticalAlignment="Top"></Label> <TextBox x:Name="txtOil" Grid.Row="1" Grid.Column="1" VerticalAlignment="Top" TextChanged="txt1_TextChanged" > </TextBox> <Label x:Name="lblRice" Content="Rice" Grid.Row="2" Grid.Column="0" VerticalAlignment="Top"></Label> <TextBox x:Name="txtRice" Grid.Row="2" Grid.Column="1" VerticalAlignment="Top" TextChanged="txt2_TextChanged"> </TextBox> <Label x:Name="lblCoconut" Content="Cocunut" Grid.Row="3" Grid.Column="0" VerticalAlignment="Top"></Label> <TextBox x:Name="txtCoconut" Grid.Row="3" Grid.Column="1" VerticalAlignment="Top" TextChanged="txt3_TextChanged" > </TextBox> <Label x:Name="lblDal" Content="Dal" Grid.Row="4" Grid.Column="0" VerticalAlignment="Top"></Label> <TextBox x:Name="txtDal" Grid.Row="4" Grid.Column="1" VerticalAlignment="Top" TextChanged="txt4_TextChanged"> </TextBox> <Label x:Name="lblTotal" Content="Total" Grid.Row="5" Grid.Column="0" VerticalAlignment="Top"></Label> <TextBox x:Name="txtTotal" Grid.Row="5" Grid.Column="1" VerticalAlignment="Top" > </TextBox> <Label x:Name="lbltax" Content="Tax" Grid.Row="6" Grid.Column="0" VerticalAlignment="Top"></Label> <TextBox x:Name="txttax" Grid.Row="6" Grid.Column="1" VerticalAlignment="Top" > </TextBox> <Label x:Name="lblvat" Content="gst" Grid.Row="7" Grid.Column="0" VerticalAlignment="Top"></Label> <TextBox x:Name="txtvat" Grid.Row="7" Grid.Column="1" VerticalAlignment="Top" > </TextBox> </Grid> <!--<Button x:Name="btnUpdate" Width="100" Height="20" HorizontalAlignment="Center" Grid.Row="1" Content="Update" Command="{Binding Path=UpdateCommand}" ></Button> <ListView x:Name="lstPerson" Grid.Row="2" ItemsSource="{Binding Persons}"> <ListView.View> <GridView> <GridViewColumn Header="Materials" DisplayMemberBinding="{Binding Name}" /> <GridViewColumn Header="Price" Width="200" DisplayMemberBinding="{Binding Address}"/> </GridView> </ListView.View> </ListView>--> </Grid> </UserControl>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace task2.Views { /// <summary> /// Interaction logic for calc.xaml /// public partial class calc : UserControl { public calc() { InitializeComponent(); } protected void txt1_TextChanged(object sender, EventArgs e) { if ((!string.IsNullOrEmpty(txtOil.Text)) && (!string.IsNullOrEmpty(txtRice.Text)) && (!string.IsNullOrEmpty(txtCoconut.Text)) && (!string.IsNullOrEmpty(txtDal.Text))) { txtTotal.Text = (Convert.ToInt32(txtOil.Text) + Convert.ToInt32(txtRice.Text) + Convert.ToInt32(txtCoconut.Text) + Convert.ToInt32(txtDal.Text)).ToString(); } CalculateTax(); } protected void txt2_TextChanged(object sender, EventArgs e) { if ((!string.IsNullOrEmpty(txtOil.Text)) && (!string.IsNullOrEmpty(txtRice.Text)&& (!string.IsNullOrEmpty(txtCoconut.Text))&& (!string.IsNullOrEmpty(txtDal.Text)))) { txtTotal.Text = (Convert.ToInt32(txtOil.Text) + Convert.ToInt32(txtRice.Text) + Convert.ToInt32(txtCoconut.Text) + Convert.ToInt32(txtDal.Text)).ToString(); } CalculateTax(); } protected void txt3_TextChanged(object sender, EventArgs e) { if ((!string.IsNullOrEmpty(txtOil.Text)) && (!string.IsNullOrEmpty(txtRice.Text)) && (!string.IsNullOrEmpty(txtCoconut.Text)) && (!string.IsNullOrEmpty(txtDal.Text))) { txtTotal.Text = (Convert.ToInt32(txtOil.Text) + Convert.ToInt32(txtRice.Text) + Convert.ToInt32(txtCoconut.Text) + Convert.ToInt32(txtDal.Text)).ToString(); } CalculateTax(); } protected void txt4_TextChanged(object sender, EventArgs e) { if ((!string.IsNullOrEmpty(txtOil.Text)) && (!string.IsNullOrEmpty(txtRice.Text)) && (!string.IsNullOrEmpty(txtCoconut.Text)) && (!string.IsNullOrEmpty(txtDal.Text))) { txtTotal.Text = (Convert.ToInt32(txtOil.Text) + Convert.ToInt32(txtRice.Text) + Convert.ToInt32(txtCoconut.Text) + Convert.ToInt32(txtDal.Text)).ToString(); } CalculateTax(); } public void CalculateTax() { if ((!string.IsNullOrEmpty(txtTotal.Text))) { int Amount = Convert.ToInt32(txtTotal.Text); txtvat.Text = (((Amount * 12) / 100) + Amount).ToString(); if (Amount > 2000) { txttax.Text = "20"; } else if (Amount > 1000) { txttax.Text = "10"; } else { txttax.Text = "5"; } } } public bool IsValid() { int Amount = Convert.ToInt32(txtTotal.Text); if (Amount == 0) { return false; } else { return true; } } } }