(-: Fibergeek's Blog :-)
Inhoud blog
  • Python 2 of 3
  • IDA-python: GetStructureIdAt
  • Geslaagd in Microsoft's 70-680 examen
  • minibio: kort vervolg
  • miniboa: een voorbeeld
    Zoeken in blog

    Categorieën
  • Code: C/C++ (2)
  • Code: Powershell (1)
  • Code: Python (8)
  • Code: WPF (2)
  • Programmeren (5)
  • 07-06-2011
    Klik hier om een link te hebben waarmee u dit artikel later terug kunt lezen.Mijn begin met WPF (Data Binding)
    Het is nu al bijna een jaar geleden dat ik met WPF (Windows Presentation Foundation) begonnen ben. Echter, ik ben het meeste al weer vergeten. Het is aanpassen . Eens de "basics" van Data-Binding herbekijken...

    Een simpel programma, een tekst in het midden van het scherm:

    <

    Application x:Class="Application"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                
    StartupUri="MainWindow.xaml">
    </
    Application>
    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns
    :x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Een simpele test" Height="350" Width="525">
           
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25">
             
    Dit is een simpele test...
           
    </TextBlock>
    </
    Window>


    Dit is dezelfde versie van het programma, maar de tekst is nu verhuist naar een Resource-blok op het Window-niveau:

    <Application x:Class="Application"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                
    StartupUri="MainWindow.xaml">
    </
    Application>
    <

    Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:s="clr-namespace:System;assembly=mscorlib"
            Title="Een simpele test" Height="350" Width="525">
      <Window.Resources>
        <s:String x:Key="TestTekst">Dit is een simpele test...</s:String>
      </Window.Resources>
      <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"
                 Text="{Binding Source={StaticResource TestTekst}}"
                />
    </
    Window>


    We kunnen de TestTekst-variable ook verhuizen naar het Application-blok (met als voordeel dat deze variable nu gebruikt kan worden in andere schermen):

    <Application x:Class="Application"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:s="clr-namespace:System;assembly=mscorlib"
                
    StartupUri="MainWindow.xaml">
      <Application.Resources>
        <s:String x:Key="TestTekst">Dit is een simpele test...</s:String>
      </Application.Resources>
    </
    Application>
    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Een simpele test" Height="350" Width="525">
      <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"
                 Text="{Binding Source={StaticResource TestTekst}}"
                />
    </
    Window>


    Het is ook mogelijk om deze tekst te laten komen uit een variable uit de bijhorende Window-class. De DataContext van het window moet dan naar zichzelf wijzen:

    <Application x:Class="Application"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                
    StartupUri="MainWindow.xaml">
    </
    Application>
    <

    Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"
           
    Title="Een simpele test" Height="350" Width="525">
      <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"
                
    Text="{Binding Path=TestTekst}"
                />
    </
    Window>

    De bijhorende classe-code:

    Class

    MainWindow
     
    Public Property TestTekst As String = "Dit is een simpele test..."
    End Class


    Maar omdat de Designer nu de inhoud van de TestTekst zelf niet kan bepalen wordt deze niet weergegeven. Vreed lastig dus tijdens de ontwikkeling van de schermen. Dit kan men oplossen door een FallbackValue in te stellen. Hetzelfde programma als hierboven maar het TextBlock in het Window-blok is een klein beetje aangepast:

    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"
           
    Title="Een simpele test" Height="350" Width="525">
      <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"
                 T
    ext="{Binding Path=TestTekst,FallbackValue='(hier komt de TestTekst)'}"
                />
    </
    Window>

    07-06-2011, 00:00 Geschreven door Fibergeek  


    Categorie:Code: WPF
    Archief per week
  • 25/11-01/12 2013
  • 05/11-11/11 2012
  • 07/05-13/05 2012
  • 05/03-11/03 2012
  • 20/02-26/02 2012
  • 13/02-19/02 2012
  • 30/01-05/02 2012
  • 12/12-18/12 2011
  • 05/12-11/12 2011
  • 19/09-25/09 2011
  • 15/08-21/08 2011
  • 01/08-07/08 2011
  • 04/07-10/07 2011
  • 06/06-12/06 2011

    E-mail mij

    Druk op onderstaande knop om mij te e-mailen.


    Gastenboek

    Druk op onderstaande knop om een berichtje achter te laten in mijn gastenboek


    Blog als favoriet !


    Blog tegen de wet? Klik hier.
    Gratis blog op https://www.bloggen.be - Meer blogs