开发者

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

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

I have a text box in which text is automatically generated . Requi开发者_JS百科rement: 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 ..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜