WPF学習データバインディング

オリジナル: WPF学習用のデータバインディング

コードをコピー
<StackPanel Name = " StackPanelOut " >
    <StackPanel Name = " StackPanelIn " >
        <!-バインディング要素->
        <Slider Name = " sliderFont " Maximum = " 40 " Minimum = " 10 " Value = " 20 " TickPlacement = " BottomRight "   TickFrequency = " 1 " IsSnapToTickEnabled = " True " Height = " Auto " Margin = " 5 " > </スライダー>
        <!-プロパティを前の要素(ElementName)、バインディング値(Path)、バインディングモード(デフォルトは一方向)にバインドします->
        <Label FontSize = " {Binding ElementName = sliderFont、Path = Value、Mode = TwoWay} " Content = " {Binding ElementName = sliderFont、Path = Value} " > </ Label>

        <!-属性を非要素リソースにバインド->
        <Label Content = " {Binding Source = {x:Static SystemFonts.IconFontFamily}、Path = Source} " />
        <!-静的リソースにバインド->
        <Label Content = " {Binding Source = {StaticResource MyFont}、Path = Source} " > </ Label>
        <!-カスタムリソースにバインド->
        <レーベル>
            <!-親クラスにバインドされたデータ->
            <Label.Content>
                <Binding Path = " Title " >
                    <!-親クラスにバインドされた値->
                    <Binding.RelativeSource>
                        <!-バインディングモード、バインディングのタイプ->
                        <RelativeSource Mode = " FindAncestor " AncestorType = " {x:Type Window} " />
                    </Binding.RelativeSource>
                </ Binding>
            </Label.Content>
        </ラベル>
        <!-以前の略語によると、AncestorLevelは要素ツリーを検索します->
        <Label Content = " {Binding Path = Name、RelativeSource = {RelativeSource Mode = FindAncestor、AncestorType = {x:Type StackPanel}、AncestorLevel = 2}} " />

        <!-複数の要素、同じデータをバインドするときの処理(同じソースの処理)->
        <Label Content = " {Binding Source = {x:Static SystemFonts.IconFontFamily}、Path = Source} " />
        <Label Content = " {Binding Source = {x:Static SystemFonts.IconFontFamily}、Path = LineSpacing} " />
        <Label Content = " {Binding Source = {x:Static SystemFonts.IconFontFamily}、Path = FamilyTypefaces [0] .Style} " />           
        <!-上にバインドされたデータを親のDataContextに保存します(上位の親にすることもできます)->
        <StackPanel DataContext = " {x:Static SystemFonts.IconFontFamily} " >
            <!-Empty Source、WPFは、親DataContextがnullではないデータを自動的に検索して適用します。見つからないか間違っている場合、表示されません->
            <Label Content = " {Binding Path = Source} " />
            <Label Content = " {Binding Path = LineSpacing} " />
            <Label Content = " {Binding Path = FamilyTypefaces [0] .Style} " />
        </ StackPanel>
    </ StackPanel>
</ StackPanel>
コードをコピー

 

おすすめ

転載: www.cnblogs.com/lonelyxmas/p/12749274.html