Introduction to the second part of WPF

Source: Internet
Author: User

3. In the data-triggered ririgger program, Beijing will encounter certain judgment Conditions Based on Data Execution. In this case, we can consider using DataTriger. The Binding attribute of the DataTriger object will continuously send data. Once the sent Value is consistent with the Value attribute, the DataTriger will be triggered. [Csharp] using System; using System. windows. data; namespace WpfApp4.Converters {class StringToBooleanConverter: IValueConverter {public object Convert (object value, Type targetType, object parameter, System. globalization. cultureInfo culture) {string str = value as string; if (! String. isNullOrEmpty (str) & str. length> 6) return false; else return true;} public object ConvertBack (object value, Type targetType, object parameter, System. globalization. cultureInfo culture) {throw new NotImplementedException () ;}} using System; using System. windows. data; namespace WpfApp4.Converters {class StringToBooleanConverter: IValueConverter {public object Convert (object value, Type TargetType, object parameter, System. Globalization. CultureInfo culture) {string str = value as string; if (! String. isNullOrEmpty (str) & str. length> 6) return false; else return true;} public object ConvertBack (object value, Type targetType, object parameter, System. globalization. cultureInfo culture) {throw new NotImplementedException () ;}} [html] <Window x: Class = "WpfApp4.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xam L "xmlns: converters =" clr-namespace: WpfApp4.Converters "Title =" MainWindow "Height =" 350 "Width =" 525 "> <Window. resources> <converters: StringToBooleanConverter x: Key = "cvtr"/> <Style TargetType = "{x: Type TextBox}"> <Style. triggers> <DataTrigger Binding = "{Binding RelativeSource = {x: Static Member = RelativeSource. self}, Path = Text, Converter = {StaticResource cvtr} "Value =" false "> <Setter Property =" BorderBrus H "Value =" Red "/> <Setter Property =" BorderThickness "Value =" 1 "/> </DataTrigger> </Style. triggers> </Style> </Window. resources> <StackPanel> <TextBox Margin = "5"/> </StackPanel> </Window> <Window x: Class = "WpfApp4.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: converters = "clr-namespace: WpfApp4.Converters" Title = "MainWindow" Height = "350" Width = "525"> <Window. resources> <converters: StringToBooleanConverter x: Key = "cvtr"/> <Style TargetType = "{x: Type TextBox}"> <Style. triggers> <DataTrigger Binding = "{Binding RelativeSource = {x: Static Member = RelativeSource. self}, Path = Text, converter = {StaticResource cvtr} "Value =" false "> <Setter Property =" BorderBrush "Value =" Red "/> <Setter Property =" BorderThickness "Value =" 1 "/> </DataTrigger> </Style. triggers> </Style> </Window. resources> <StackPanel> <TextBox Margin = "5"/> </StackPanel> </Window> 4. multiDataTrigger triggered by multiple data conditions can trigger changes only when multiple data conditions are met. [Html] <Window x: Class = "WpfApp4.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: converters = "clr-namespace: wpfApp4.Converters "Title =" MainWindow "Height =" 350 "Width =" 525 "> <Window. resources> <converters: StringToBooleanConverter x: Key = "cvtr"/> <Style TargetType = "{x: Type ListBoxItem}"> <Setter Property = "ContentTemplate"> <Setter. value> <DataTemplate> <StackPanel Orientation = "Horizontal"> <TextBlock Text = "{Binding ID}" Width = "60"/> <TextBlock Text = "{Binding Name }" width = "120"/> <TextBlock Text = "{Binding Age}" Width = "60"/> </StackPanel> </DataTemplate> </Setter. value> </Setter> <Style. triggers> <MultiDataTrigger. conditions> <Condition Binding = "{Binding Path = ID}" Value = "2"/> <Condition Binding = "{Binding Path = Name}" Value = "Tom"/> </MultiDataTrigger. conditions> <MultiDataTrigger. setters> <Setter Property = "Background" Value = "Orange"/> </MultiDataTrigger. setters> </MultiDataTrigger> </Style. triggers> </Style> </Window. resources> <StackPanel> <ListBox x: Name = "listBoxStudent" Margin = "5"/> </StackPanel> </Window> <Window x: class = "WpfApp4.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: converters = "clr-namespace: wpfApp4.Converters "Title =" MainWindow "Height =" 350 "Width =" 525 "> <Window. resources> <converters: StringToBooleanConverter x: Key = "cvtr"/> <Style TargetType = "{x: Type ListBoxItem}"> <Setter Property = "ContentTemplate"> <Setter. value> <DataTemplate> <StackPanel Orientation = "Horizontal"> <TextBlock Text = "{Binding ID}" Width = "60"/> <TextBlock Text = "{Binding Name }" width = "120"/> <TextBlock Text = "{Binding Age}" Width = "60"/> </StackPanel> </DataTemplate> </Setter. value> </Setter> <Style. triggers> <MultiDataTrigger. conditions> <Condition Binding = "{Binding Path = ID}" Value = "2"/> <Condition Binding = "{Binding Path = Name}" Value = "Tom"/> </MultiDataTrigger. conditions> <MultiDataTrigger. setters> <Setter Property = "Background" Value = "Orange"/> </MultiDataTrigger. setters> </MultiDataTrigger> </Style. triggers> </Style> </Window. resources> <StackPanel> <ListBox x: Name = "listBoxStudent" Margin = "5"/> </StackPanel> </Window> [csharp] public partial class MainWindow: window {List <Student> stus = new List <Student> () {new Student () {ID = 1, Name = "Anders", Age = 27}, new Student () {ID = 2, Name = "Tom", Age = 22}, new Student () {ID = 3, Name = "Jim", Age = 23}, new Student () {ID = 4, Name = "Jack", Age = 24}, new Student () {ID = 5, Name = "Mary", Age = 25 },}; public MainWindow () {www.2cto.com InitializeComponent (); this. listBoxStudent. itemsSource = this. stus ;}} public class Student {public int ID {get; set ;}public string Name {get; set ;}public int Age {get; set ;}}

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.