Binding

El enlace de datos es un mecanismo que establece una conexión entre la interfaz gráfica de usuario de una aplicación y los datos proporcionados por objetos pertenecientes a la lógica de negocio de dicha aplicación

Ejemplo 1:

<Slider Name="MiSlide" HorizontalAlignment="Left" 
   Margin="99,55,0,0" VerticalAlignment="Top" 
   Height="24" Width="279" Value="10" 
   Maximum="40" Minimum="10" TickFrequency="1" 
   IsSnapToTickEnabled="True" TickPlacement="TopLeft"/>

 <TextBlock Name="LblEjemploTexto" 
   FontSize="{Binding ElementName=MiSlide, Path=Value,Mode=TwoWay}" 
   HorizontalAlignment="Left" Margin="99,128,0,0" 
   TextWrapping="Wrap" Text="Esto es un ejemplo de Binding" 
   VerticalAlignment="Top" Height="47" Width="279" FontWeight="Bold"/>

Ejemplo 2:

<Slider Name="MiSlide" HorizontalAlignment="Left" 
 Margin="10,30,0,0" VerticalAlignment="Top" 
 Height="24" Width="497" Value="10" 
 Maximum="40" Minimum="10" TickFrequency="1" 
 IsSnapToTickEnabled="True" TickPlacement="TopLeft"/>

<TextBlock Name="LblEjemploTexto" 
 FontSize="{Binding ElementName=MiSlide, Path=Value,Mode=TwoWay}" 
 HorizontalAlignment="Left" Margin="10,73,0,0" 
 TextWrapping="Wrap" Text="Esto es un ejemplo de Binding" 
 VerticalAlignment="Top" Height="58" Width="497" FontWeight="Bold"/>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="30" Margin="10,131,0,0" VerticalAlignment="Top" Width="497">
   <Button Name="BtnPequeno" Content="Pequeño" Padding="3" Margin="2"/>
   <Button Name="BtnNormal" Content="Normal" Padding="3" Margin="2"/>
   <Button Name="BtnGrande" Content="Grande" Padding="3" Margin="2"/>
 </StackPanel>
 <Label Content="Tamaño exacto:" 
    HorizontalAlignment="Left" Margin="10,168,0,0" VerticalAlignment="Top"/>
 <TextBox Name="TxtExacto" HorizontalAlignment="Left" Height="23" Margin="108,172,0,0" TextWrapping="Wrap" 
 Text="{Binding ElementName=LblEjemploTexto, Path=FontSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
 VerticalAlignment="Top" Width="38"/>

En el CodeBegind implementamos la funcionalidad de los botones

 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CmdSetPeque(object sender, RoutedEventArgs e)
        {
            this.MiSlide.Value = 10;
        }
        private void CmdSetNormal(object sender, RoutedEventArgs e)
        {
            this.MiSlide.Value = 24;
        }
        private void CmdSetGrande(object sender, RoutedEventArgs e)
        {
            this.MiSlide.Value = 34;
        }
    }

Ejemplo 3:

 <Slider Name="MiSlide" HorizontalAlignment="Left" 
 Margin="10,30,0,0" VerticalAlignment="Top" 
 Height="24" Width="497" Value="10" 
 Maximum="40" Minimum="10" TickFrequency="1" 
 IsSnapToTickEnabled="True" TickPlacement="TopLeft"/>
 <TextBlock Name="LblEjemploTexto" 
 FontSize="{Binding ElementName=MiSlide, Path=Value,Mode=TwoWay}" 
 Foreground="{Binding ElementName=LstColores, Path=SelectedItem.Content}"
 HorizontalAlignment="Left" Margin="10,73,0,0" 
 TextWrapping="Wrap" Text="Esto es un ejemplo de Binding" 
 VerticalAlignment="Top" Height="58" Width="497" FontWeight="Bold"/>

 <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="30" Margin="10,131,0,0" VerticalAlignment="Top" Width="497">
     <Button Name="BtnPequeno" Content="Pequeño" Padding="3" Margin="2" Click="CmdSetPeque"/>
     <Button Name="BtnNormal" Content="Normal" Padding="3" Margin="2" Click="CmdSetNormal"/>
     <Button Name="BtnGrande" Content="Grande" Padding="3" Margin="2" Click="CmdSetGrande"/>
 </StackPanel>

<Label Content="Tamaño exacto:" HorizontalAlignment="Left" Margin="10,168,0,0" VerticalAlignment="Top"/>

<TextBox Name="TxtExacto" HorizontalAlignment="Left" Height="23" Margin="108,172,0,0" TextWrapping="Wrap" 
 Text="{Binding ElementName=LblEjemploTexto, Path=FontSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
 VerticalAlignment="Top" Width="38"/>

 <ListBox Name="LstColores" HorizontalAlignment="Left" Height="136" Margin="207,136,0,0" VerticalAlignment="Top" Width="91" FontSize="18">
     <ListBoxItem Content="Green"/>
     <ListBoxItem Content="Blue"/>
     <ListBoxItem Content="Red"/>
     <ListBoxItem Content="Black"/>
 </ListBox>

Ejemplo 4: Vamos a añadir un campo para seleccionar las fuentes de letras del sistema.

 

<Slider Name="MiSlide" HorizontalAlignment="Left"
Margin="10,30,0,0" VerticalAlignment="Top"
Height="24" Width="497" Value="10"
Maximum="40" Minimum="10" TickFrequency="1"
IsSnapToTickEnabled="True" TickPlacement="TopLeft"/>
<TextBlock Name="LblEjemploTexto"
FontSize="{Binding ElementName=MiSlide, Path=Value,Mode=TwoWay}"
Foreground="{Binding ElementName=LstColores, Path=SelectedItem.Content}"
FontFamily="{Binding ElementName=LstFuentes, Path=SelectedItem}"
HorizontalAlignment="Left" Margin="10,73,0,0"
TextWrapping="Wrap" Text="Esto es un ejemplo de Binding"
VerticalAlignment="Top" Height="58" Width="497" FontWeight="Bold"/>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="30" Margin="10,131,0,0" VerticalAlignment="Top" Width="497">
    <Button Name="BtnPequeno" Content="Pequeño" Padding="3" Margin="2" Click="CmdSetPeque"/>
    <Button Name="BtnNormal" Content="Normal" Padding="3" Margin="2" Click="CmdSetNormal"/>
    <Button Name="BtnGrande" Content="Grande" Padding="3" Margin="2" Click="CmdSetGrande"/>
</StackPanel>

<Label Content="Tamaño exacto:" HorizontalAlignment="Left" Margin="10,168,0,0" VerticalAlignment="Top"/>

<TextBox Name="TxtExacto" HorizontalAlignment="Left" Height="23" Margin="108,172,0,0" TextWrapping="Wrap"
Text="{Binding ElementName=LblEjemploTexto, Path=FontSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Top" Width="38"/>

<ListBox Name="LstColores" HorizontalAlignment="Left" Height="136" Margin="207,136,0,0" VerticalAlignment="Top" Width="91" FontSize="18">
    <ListBoxItem Content="Green"/>
    <ListBoxItem Content="Blue"/>
    <ListBoxItem Content="Red"/>
    <ListBoxItem Content="Black"/>
</ListBox>

<ListBox Name="LstFuentes" HorizontalAlignment="Left" Height="136" Margin="317,136,0,0" VerticalAlignment="Top" Width="190"
ItemsSource="{x:Static Fonts.SystemFontFamilies}"/>

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.