WPF Note 1

Source: Internet
Author: User
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
}


}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.