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; }
    }
}
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM