欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF數(shù)據(jù)綁定Binding的用法

 更新時間:2022年04月21日 10:37:27   作者:農(nóng)碼一生  
這篇文章介紹了WPF數(shù)據(jù)綁定Binding的用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、簡介

WPF的核心理念是變傳統(tǒng)的UI驅(qū)動數(shù)據(jù)變成數(shù)據(jù)驅(qū)動UI,支撐這個理念的基礎(chǔ)就是本章講的Data Binding和與之相關(guān)的數(shù)據(jù)校驗和數(shù)據(jù)轉(zhuǎn)換。在使用Binding的時候,最重要的就是設(shè)置它的源和路徑。
Bingding的源:
有三個屬性用來設(shè)置源:ElementName(string)、Source(Object) 和 RelativeSource(RelativeSource)。注:這三個只能指定一個,否則異常。
ElementName:源為一個元素(Element),這里用的是此元素中設(shè)置的Name屬性。
Source:以object作為源。<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>
RelativeSource:源相對于綁定目標(biāo)的位置。
源是元素本身:{Binding RelativeSource={RelativeSource Self}};
源是Tempalte中元素的Parent:{Binding RelativeSource={RelativeSource TemplatedParent}};
源是綁定以collection形式的前一個數(shù)據(jù):{Binding RelativeSource={RelativeSource PreviousData}},MSDN上關(guān)于PreviousData的說明并不多。
以上三項為RelativeSource中的Static值,使用這些值可以減少內(nèi)存開銷。
源是Ancestor(可能比parent還高):{Binding RelativeSource={RelativeSource FindAncestor,AncestorLevel=n, AncestorType={x:Type desiredType}}}

Bingding的Path:
Binding中的Path是 PropertyPath對象。
在最簡單的情況下,Path 屬性值是要用于綁定的源對象的屬性名稱,如 Path=PropertyName。
通過類似于C#中使用的語法,可以指定屬性的子屬性。例如,子句 Path=ShoppingCart.Order 將綁定設(shè)置為對象的子屬性 Order 或?qū)傩?ShoppingCart。
若要綁定到附加屬性,請將附加屬性用括號括起。例如,若要綁定到附加屬性 DockPanel.Dock,則語法為 Path=(DockPanel.Dock)。
在應(yīng)用了索引器的屬性名稱之后的方括號內(nèi),可以指定屬性的索引器。例如,子句 Path=ShoppingCart[0] 將綁定設(shè)置為與屬性的內(nèi)部索引處理文本字符串“0”的方式對應(yīng)的索引。此外,還支持多個索引器。
在 Path 子句中可以同時使用索引器和子屬性,例如,Path=ShoppingCart.ShippingInfo[MailingAddress,Street]。
在索引器內(nèi)部,可以有多個由逗號(,) 分隔的索引器參數(shù)??梢允褂脠A括號指定每個參數(shù)的類型。例如,可以使用 Path="[(sys:Int32)42,(sys:Int32)24]",其中 sys 映射到 System 命名空間。
如果源為集合視圖,則可以用斜杠(/) 指定當(dāng)前項。例如,子句 Path=/ 設(shè)置到視圖中當(dāng)前項的綁定。如果源為集合,則此語法指定默認集合視圖的當(dāng)前項。
可以結(jié)合使用屬性名和斜杠來遍歷作為集合的屬性。例如,Path=/Offices/ManagerName 指定源集合的當(dāng)前項,該源集合包含同樣是集合的 Offices 屬性。其當(dāng)前項是包含 ManagerName 屬性的對象。
也可以使用句點(.)路徑綁定到當(dāng)前源。例如,Text=”{Binding}” 等效于 Text=”{Binding Path=.}”。

二、案例

案例一:

Grid代碼:

   <Grid>
        <!--行-->
        <Grid.RowDefinitions>
            <RowDefinition Height="200" />
            <RowDefinition Height="200"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--列-->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <!--設(shè)置在Grid中布局位置-->
        <!--將MainWindow分為3x3的布局,則位于中間(1,1)位置,個人理解相當(dāng)于坐標(biāo)。-->
        <StackPanel  Grid.Row="1" Grid.Column="1">
            <TextBlock Width="250" Height="30" Text="顏色:" TextWrapping="Wrap"  Background="Azure" FontSize="20"/>
            <ListBox x:Name="listColor" Width="200" Height="90"  Background="Azure"  FontSize="20">
                <ListBoxItem Content="Blue"/>
                <ListBoxItem Content="Red"/>
                <ListBoxItem Content="Green"/>
                <ListBoxItem Content="Gray"/>
                <ListBoxItem Content="Cyan"/>
                <ListBoxItem Content="GreenYellow"/>
                <ListBoxItem Content="Orange"/>
            </ListBox>
            <TextBlock Width="200" Height="30" TextWrapping="Wrap"  FontSize="20" Margin="3,1,2,1"   Background="Azure" Text="改變背景色:" />
            <!--綁定選擇值-->
            <TextBlock Width="200" Height="30" Text="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}"  Background="{Binding ElementName=listColor, Path=SelectedItem.Content, Mode=OneWay}">
            </TextBlock>
            <TextBox Name="txtTwoWay" Text="{Binding ElementName=listColor,Path=SelectedItem.Content,Mode=TwoWay}"  Background="{Binding ElementName=listColor,  Path=SelectedItem.Content,Mode=TwoWay}" >
            </TextBox>
        </StackPanel>
    </Grid>

執(zhí)行效果:

案例二:

Grid代碼:

<Grid>
        <StackPanel>
            <TextBlock Text="{Binding ElementName=slider,Path=Value}"/>
            <Slider x:Name="slider" Maximum="100" Minimum="1"/>
        </StackPanel>
 </Grid> 

執(zhí)行效果:

三、總結(jié)

在上例子中綁定數(shù)據(jù)源時候用到了Mode屬性,通過查閱資料得知有以下區(qū)別:
oneTime:一次性綁定,將數(shù)據(jù)給控件,綁定就結(jié)束。
oneWay:數(shù)據(jù)源改變會影響綁定該數(shù)據(jù)源的控件。
twoWay:數(shù)據(jù)源改變會影響綁定該數(shù)據(jù)源的控件,并且控件中數(shù)據(jù)改變時也會影響到數(shù)據(jù)源。

到此這篇關(guān)于WPF數(shù)據(jù)綁定Binding的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論