Scenario: If there is no content in the "comment content" TextBox, the "Submit" Button cannot be clicked. This is a typical OneWay data Binding, because only TextBox affects the part of the Button.
XAML:
<Window x: Class = "Expense. Window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "Window1" Height = "300" Width = "300">
<Grid>
<Grid. Background>
<LinearGradientBrush>
<GradientStop Offset = "0" Color = "Blue"> </GradientStop>
<GradientStop Offset = "0.5" Color = "LightBlue"> </GradientStop>
<GradientStop Offset = "1" Color = "DarkSlateBlue"> </GradientStop>
</LinearGradientBrush>
</Grid. Background>
<TextBox Height = "23" Margin = "10, 10, 0" Name = "textBox1" verticalignment = "Top" Text = "{Binding ElementName = slider1, Path = Value, updateSourceTrigger = PropertyChanged, Mode = TwoWay} "> </TextBox>
<Slider Height = "21" Margin = "10, 40, 10, 0" Name = "slider1" verticalignment = "Top" Maximum = "100"/>
<Button Height = "23" HorizontalAlignment = "Right" Margin = "0, 0, 10, 80 "Name =" IsVoid "verticalignment =" Bottom "Width =" 75 "Click =" IsVoid_Click "> IsVoid </Button>
<TextBox Margin = "10,104," Name = "textBox2" Height = "20" verticalignment = "Top"/>
</Grid>
</Window>
Cs code:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
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 Expense
{
/// <Summary>
/// Interaction logic of Window1.xaml
/// </Summary>
Public partial class Window1: Window
{
Public Window1 ()
{
InitializeComponent ();
IsVoid. IsEnabled = false;
Binding B = new Binding ("Text ");
B. Mode = BindingMode. OneWay;
B. Source = textBox2;
B. Converter = new ConverterText2Button ();
This. IsVoid. SetBinding (Button. IsEnabledProperty, B );
}
Private void IsVoid_Click (object sender, RoutedEventArgs e)
{
MessageBox. Show (textBox2.Text );
}
}
[ValueConversion (typeof (string), typeof (bool?)]
Class ConverterText2Button: IValueConverter
{
# Region IValueConverter Member
Public object Convert (object value, Type targetType, object parameter, System. Globalization. CultureInfo culture)
{
String str = System. Convert. ToString (value );
If (str = String. Empty)
{
Return false;
}
Else
{
Return true;
}
Throw new NotImplementedException ();
}
Public object ConvertBack (object value, Type targetType, object parameter, System. Globalization. CultureInfo culture)
{
Throw new NotImplementedException ();
}
# Endregion
}
}