How to transfer control from one window to another without the use of an event handler?

382 Views Asked by At

Im new to programming and in need clarification for the following ...

I have a text box in which text is automatically generated . Requirement: If i now highlight the text in the text box a new wpf window should open .. (this needs to be done using either wpf commands on attached property only/ not events)

Thanks :) P.s please give me detailed code for a reply ..

1

There are 1 best solutions below

0
On

This is a very strange requirement but it can be done using behaviors. Here is some sample markup:

<Grid>
    <TextBox Text="This is some text">
        <i:Interaction.Behaviors>
            <local:NewWindowOnSelectBehavior/>
        </i:Interaction.Behaviors>
    </TextBox>
</Grid>

and here is the behavior that for demonstration purposes shows a message box:

public class NewWindowOnSelectBehavior : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.SelectionChanged += (s, e) =>
        {
            if (!string.IsNullOrEmpty(AssociatedObject.SelectedText))
                MessageBox.Show("New Window");
        };
    }
}

This example uses behaviors. If you are not familiar with behaviors, install the Expression Blend 4 SDK and add this namespace:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

and add System.Windows.Interactivity to your project.