Formular una preguntaFormular una pregunta
 

RespondidaHow to copy text with trigger

  • martes, 03 de noviembre de 2009 22:03MauroGv Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código
    Hi everyone,
    I have two textboxes and I would copy the text from one to other when in third control Checkbox, property IsChecked becomes true.
    I would get this operation using triggers avoiding event handling.
    My question is if it possible to access in trigger to the properties of other controls.
    The outline code is below:
    <Window x:Class="Wizard.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="300" Width="300">
        <Grid>
            <Grid.Resources>
                 <Style x:Key="CheckBoxStyle" TargetType="{x:Type CheckBox}">
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter ..... />
                    </Trigger>
                </Style.Triggers>
            </Style>
            </Grid.Resources>
            
            <TextBox Height="23" Margin="42,28,116,0" Name="textBox1" VerticalAlignment="Top" Text="Text to Copy"/>
            <TextBox Height="23" Margin="42,75,116,0" Name="textBox2" VerticalAlignment="Top" />
            <CheckBox Height="18" Margin="0,0,0,112" Name="copyTextCheckBox" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="278"
                      Style="{StaticResource CheckBoxStyle}">
                <TextBlock TextWrapping="Wrap"  Height="28" Margin="0,0,0,0"
                           Name="checkBox1" Text="Copy Text from TextBox1 to TextBox2 with Trigger"/>
            </CheckBox>
        </Grid>
    </Window><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/>
    







    T


    Thanks in advance,
    Mauro
    Mauro
    • CambiadoMark Wilson-ThomasMSFTviernes, 06 de noviembre de 2009 2:18WPF Framework question (From:Visual Studio WPF Designer)
    •  

Respuestas

  • sábado, 07 de noviembre de 2009 15:40Linda LiuMSFT, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Hi Mauro,

    > My question is if it possible to access in trigger to the properties of other controls.

    Yes, but not in the way you mentioned, i.e. set a property of other control from the style of a control. The correct way is to use data binding. See the following sample for detailed information on how to do this.

    <TextBox Height="23" Margin="42,75,116,0" Name="textBox2" VerticalAlignment="Top">
             <TextBox.Style>
                    <Style TargetType="TextBox">
                        <Style.Triggers>                
                            <DataTrigger Binding="{Binding ElementName=copyTextCheckBox,Path=IsChecked}" Value="True">
                                <Setter Property="Text" Value="{Binding ElementName=textBox1,Path=Text}"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
               </TextBox.Style>
     </TextBox>

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marcado como respuestaMauroGv sábado, 07 de noviembre de 2009 19:57
    •  

Todas las respuestas

  • sábado, 07 de noviembre de 2009 15:40Linda LiuMSFT, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Hi Mauro,

    > My question is if it possible to access in trigger to the properties of other controls.

    Yes, but not in the way you mentioned, i.e. set a property of other control from the style of a control. The correct way is to use data binding. See the following sample for detailed information on how to do this.

    <TextBox Height="23" Margin="42,75,116,0" Name="textBox2" VerticalAlignment="Top">
             <TextBox.Style>
                    <Style TargetType="TextBox">
                        <Style.Triggers>                
                            <DataTrigger Binding="{Binding ElementName=copyTextCheckBox,Path=IsChecked}" Value="True">
                                <Setter Property="Text" Value="{Binding ElementName=textBox1,Path=Text}"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
               </TextBox.Style>
     </TextBox>

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marcado como respuestaMauroGv sábado, 07 de noviembre de 2009 19:57
    •  
  • sábado, 07 de noviembre de 2009 19:56MauroGv Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi Linda,
    thanks for your useful reply.
    My goal was handling all the code in xaml.
    Your code solve my problem overulling my point of view: when in CheckBox IsChecked=True is the TextBox.Style of the control that receives the Data that handles the new value with DataTrigger.  
    So no Event Trigger but Data Trigger.
    Many thanks,
    Mauro


    Mauro