Odeslat dotazOdeslat dotaz
 

OdpovědětHow to copy text with trigger

  • 3. listopadu 2009 22:03MauroGv Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Obsahuje kód
    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
    • PřesunutýMark Wilson-ThomasMSFT6. listopadu 2009 2:18WPF Framework question (From:Visual Studio WPF Designer)
    •  

Odpovědi

  • 7. listopadu 2009 15:40Linda LiuMSFT, ModerátorUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    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.
    • Označen jako odpověďMauroGv 7. listopadu 2009 19:57
    •  

Všechny reakce

  • 7. listopadu 2009 15:40Linda LiuMSFT, ModerátorUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    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.
    • Označen jako odpověďMauroGv 7. listopadu 2009 19:57
    •  
  • 7. listopadu 2009 19:56MauroGv Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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