我用mvvm模型进行编程。
我有一个文本框(txtResult),它在我的rulesengine中计算并返回到mu modelview。这样可行。 但是我的文本框中没有显示更改。
我的视图的xaml代码(在后台没有任何内容):
<Window x:Class="RulesEngineExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="177" Width="491"
xmlns:local="clr-namespace:RulesEngineExample" Loaded="Window_Loaded">
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="51*" />
<RowDefinition Height="24*" />
<RowDefinition Height="28*" />
<RowDefinition Height="24*" />
<RowDefinition Height="11*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*" />
<ColumnDefinition Width="119*" />
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="41*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="119*" >
<ColumnDefinition Width="30*" />
<ColumnDefinition Width="121*" />
<ColumnDefinition Width="18*" />
</Grid.ColumnDefinitions>
<TextBox Name="txtField1" Text="{Binding Field1, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="1" />
<TextBox Name="txtField2" Text="{Binding Field2, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="5" />
<TextBox Name="txtResult" Text="{Binding ResultCalc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="7" Grid.Row="1" IsReadOnly="True" />
<Label Height="23" Content="=" Grid.Row="1" Grid.Column="6" />
<ComboBox Height="23" SelectedItem="{Binding Operator, Mode=TwoWay}" Name="cbOperator" Grid.Row="1" Grid.Column="3">
<ComboBoxItem Content="+" />
<ComboBoxItem Content="-" />
<ComboBoxItem Content="*" />
<ComboBoxItem Content="/" />
</ComboBox>
<Button Content="Regeln bearbeiten" Height="23" Name="btn" Grid.Column="7" Grid.Row="3" Click="btn_Click" />
</Grid>
</Window>
并且有我的viewmodel代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System.ComponentModel;
using System.Windows.Media;
namespace RulesEngineExample
{
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
public MainViewModel()
{
Messenger.Default.Register<double>(this, SetResult);
Messenger.Default.Register<Brush>(this, SetFailColor);
}
private Brush brFailBrush;
public Brush FailBrush
{
get { return brFailBrush; }
set
{
brFailBrush = value;
base.RaisePropertyChanged("FailBrush");
}
}
private string strFieldOne;
public string Field1
{
get { return strFieldOne ; }
set
{
strFieldOne = value;
OnPropertyChanged("Field1");
}
}
private string strFieldTwo;
public string Field2
{
get { return strFieldTwo; }
set
{
strFieldTwo = value;
OnPropertyChanged("Field2");
}
}
private string strResult;
public string ResultCalc
{
get { return strResult; }
set
{
string strOldResult = strResult ;
strResult = value;
base.RaisePropertyChanged("ResultCalc");
//OnPropertyChangedAtVM("ResultCalc");
}
}
private string strOperator;
public string Operator
{
get { return strOperator;}
set
{
string[] s = value.Split(':');
string st = s[s.Length - 1];
strOperator = st.Trim();
OnPropertyChanged("Operator");
}
}
public void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
PropertyChangedItem item = new PropertyChangedItem()
{
Operator = this.Operator,
Field1 = this.Field1,
Field2 = this.Field2,
Result = this.ResultCalc
};
Messenger.Default.Send<PropertyChangedItem>(item);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void SetResult(double dResult)
{
this.ResultCalc = dResult.ToString();
base.RaisePropertyChanged("ResultCalc");
}
private void SetFailColor(Brush FailBrush)
{
this.FailBrush = FailBrush;
}
}
}