• WPF数据绑定


    一:控件到控件的绑定

    1:OneWay
    Source影响着Target,但是Target却影响不到Source。
    2:OneWayToSource
    Target影响Source,而Source却影响不到Target。
    3:TwoWay
    Source与Target相互影响。
    4:OneTime
    在OneWay的基础上延伸了一个OneTime,仅绑定一次。如果大家属性Jquery中的one函数我想就可以不用表述了。

    复制代码
    <Window x:Class="WpfApplication1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:src="clr-namespace:WpfApplication1"
            Title="MainWindow" Height="350" Width="525">
        <Canvas>
            <ScrollBar Height="24" Name="scrollBar1" Width="237" Orientation="Horizontal" Canvas.Left="103" Canvas.Top="51"  Minimum="1" Maximum="100" SmallChange="1" />
            <Label Canvas.Left="41" Canvas.Top="121" Content="OneWay" Height="28" Name="label1" />
            <TextBox Canvas.Left="165" Canvas.Top="121" Height="23"
                     Text="{Binding ElementName=scrollBar1, Path=Value, Mode=OneWay}"
                     Name="textBox1" Width="120" />
            <Label Canvas.Left="41" Canvas.Top="160" Content="OneWayToSource" Height="28" Name="label2" />
            <TextBox Canvas.Left="165" Canvas.Top="160" Height="23"
                       Text="{Binding ElementName=scrollBar1, Path=Value, Mode=OneWayToSource}"
                     Name="textBox2" Width="120" />
            <Label Canvas.Left="41" Canvas.Top="202" Content="TwoWay" Height="28" Name="label3" />
            <TextBox Canvas.Left="165" Canvas.Top="202" Height="23"
                       Text="{Binding ElementName=scrollBar1, Path=Value, Mode=TwoWay}"
                     Name="textBox3" Width="120" />
            <Label Canvas.Left="41" Canvas.Top="231" Content="OneTime" Height="28" Name="label4" />
            <TextBox Canvas.Left="165" Canvas.Top="231" Height="23" 
                       Text="{Binding ElementName=scrollBar1, Path=Value, Mode=OneTime}"
                     Name="textBox4" Width="120" />
        </Canvas>
    </Window>
    复制代码

     

    二:.net对象与控件的绑定

    复制代码
    <Window x:Class="WpfApplication1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:src="clr-namespace:WpfApplication1"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ListView  Height="287" HorizontalAlignment="Left" Margin="62,12,0,0" Name="listView1" VerticalAlignment="Top" Width="331">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn Header="姓名"  DisplayMemberBinding="{Binding Name}"/>
                            <GridViewColumn Header="年龄" DisplayMemberBinding="{Binding Age}"/>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Window>
    复制代码

    三:.net方法与控件的绑定( 在做wpf时,有时我们需要在xaml中绑定.net中的方法,当然这在实际开发中也是很常用的,不过方法必要由ObjectDataProvider来封装。)

    复制代码
    <Window x:Class="WpfApplication1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:WpfApplication1"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ObjectDataProvider x:Key="Test" ObjectType="{x:Type local:Student}" MethodName="GetName">
            </ObjectDataProvider>
        </Window.Resources>
        <Grid>
            <TextBlock Text="{Binding Source={StaticResource ResourceKey=Test}, Mode=OneWay}"/>
        </Grid>
    </Window>
    复制代码
    复制代码
    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.Shapes;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Window1.xaml 的交互逻辑
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
        }
    
        public class Student
        {
            //前台要引用的方法
            public string GetName()
            {
                return "WPF";
            }
        }
    }
    复制代码

    四:wpf中的验证

    复制代码
    <Window x:Class="WpfApplication1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:WpfApplication1"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:Student x:Key="student"/>
        </Window.Resources>
        <Grid>
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="97,54,0,0" Name="textBlock1" Text="姓名" VerticalAlignment="Top" />
            <TextBox DataContext="{StaticResource ResourceKey=student}" Height="23" HorizontalAlignment="Left" Margin="153,54,0,0" Name="textBox1" VerticalAlignment="Top" Width="120">
                <TextBox.Text>
                    <Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
                        <!-- 自定义的验证规格,当然可以是多个Check -->
                        <Binding.ValidationRules>
                            <local:NameCheck />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
                <TextBox.ToolTip>
                    <!--将当前的错误信息显示在tooltip上-->
                    <Binding RelativeSource="{RelativeSource Self}"  Path="(Validation.Errors)[0].ErrorContent"/>
                </TextBox.ToolTip>
            </TextBox>
        </Grid>
    </Window>
    复制代码
    复制代码
    namespace WpfApplication1
    {
        /// <summary>
        /// Window1.xaml 的交互逻辑
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
        }
    
        public class NameCheck : ValidationRule
        {
            public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
            {
                var name = Convert.ToString(value);
                //如果名字长度大于4则是非法
                if (name.Length > 4)
                    return new ValidationResult(false, "名字长度不能大于4个长度!");
                return ValidationResult.ValidResult;
            }
        }
    
        public class Student
        {
            public string Name { get; set; }
    
            public int Age { get; set; }
        }
    }
    复制代码
  • 相关阅读:
    硬件的快速迭代开发
    DAC8775芯片的使用
    单片机引脚复用使用不当会导致bug
    单片机为32bit时运算中64bit运算中遇到的bug及其规避手段
    UDS文档的阅读
    DAC124S085芯片的使用
    LSD低边驱动芯片的特点
    小时候的玩具激光灯拆解
    飞思卡尔系列单片机的censorship的使用
    TypeScript学习小结
  • 原文地址:https://www.cnblogs.com/Jeely/p/11076662.html
Copyright © 2020-2023  润新知