wp7 --绑定
<Grid>
<TextBlock Text="{Binding Source}" />
</Grid>
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
this.DataContext = App.Current.Host;
}
}
<StackPanel BindingValidationError="StackPanel_BindingValidationError" >
<StackPanel.Resources>
<my:Bills x:Name="MyBills"/>
</StackPanel.Resources>
<TextBox x:Name="MyTextBox" Width="50" Margin="10">
<TextBox.Text>
<Binding Mode="TwoWay" Source="{StaticResource MyBills}"
Path="Amount" NotifyOnValidationError="true"
ValidatesOnExceptions="true"/>
</TextBox.Text>
</TextBox>
<Button Height="50" Width="150" Content="Click To Update Source"/>
</StackPanel>
private void StackPanel_BindingValidationError(object sender,
ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
{
MyTextBox.Background = new SolidColorBrush(Colors.Red);
}
else if (e.Action == ValidationErrorEventAction.Removed)
{
MyTextBox.Background = new SolidColorBrush(Colors.White);
}
}
public class Bills
{
private double _Amount;
public double Amount
{
get { return _Amount; }
set
{
if (value < 0)
throw new Exception("Amount must be greater than zero.");
_Amount = value;
}
}
}