绑定文本框IsFocused弹出ISOPEN加上附加条件 [英] Binding Textbox IsFocused to Popup IsOpen plus additional conditions

查看:171
本文介绍了绑定文本框IsFocused弹出ISOPEN加上附加条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框弹出控制。我想在 Popup.IsOpen 属性绑定到 TextBox.IsFocused 属性。换句话说,如果文本框具有焦点,弹出是开放的。另外,如果在弹出的重点是,我不希望由于它贴近文本框失去焦点。我希望能处理这个使用绑定,而不是在事件处理程序来处理这个问题。另外,做我必须做的关于依赖属性什么,因为它们是pre-存在(即注册,重写的元数据等),或者我可以只绑定到这些属性。

下面是类似我的方案

一些示例code

 的StackPanel SP =新的StackPanel();
文本框TB =新的TextBox();
弹出弹出=新弹出();sp.Children.Add(TB);
sp.Children.Add(弹出式);
this.Content = SP;结合BD =新的绑定(IsFocused);
bd.source = tb.IsFocused;
popup.SetBinding(Popup.IsOpenProperty,BD);

从这个我是假设,如果我点击了文本框控件,把它集中,即会弹出打开,反之亦然如果文本框失去焦点,即弹出将关闭。我似乎无法得到这个工作。

如果有人有我在做什么错误的想法,也许他们也回答我的问题下半年,如果文本框失去焦点,但它是接收焦点弹出窗口,该弹出窗口将继续开放或给焦点回到文本框,这样它会留在第一结合的开放BC。当文本框失去焦点并不适用于这种场景,获得任何其它控制重点。

如果我能改写这为清楚起见,我会说这是这样的。

1)绑定 Popup.IsOpen TextBox.IsFocused

2)绑定 TextBox.IsFocused Popup.IsFocused (假设这只是将焦点回到文本框)


下面是本我的第一个C#的尝试。事情仍然不完全正确。什么也没有发生,所以我不太肯定我的错误是

 的StackPanel SP =新的StackPanel();
        文本框TB =新的TextBox();
        弹出弹出=新弹出();        文本框popupTextBox =新的TextBox();
        popup.Child = popupTextBox;
        sp.Children.Add(TB);
        sp.Children.Add(弹出式);
        this.Content = SP;
        // ***关于给予的UIElement的名称和注册它的问题
        tb.Name =siblingTextBox;
        System.Windows.NameScope.GetNameScope(TB).RegisterName(siblingTextBox,TB);        // ***关于给予的UIElement的名称和注册它的问题
        popupTextBox.Name =popupTextBox;
        System.Windows.NameScope.GetNameScope(TB).RegisterName(popupTextBox,popupTextBox);        约束力181 =新的Binding();
        binding.ElementName = tb.Name;
        popup.PlacementTarget = TB;        款式风格=新样式();
        style.TargetType = typeof运算(弹出);        DataTrigger dataTrigger =新DataTrigger();
        结合focusedBinding =新的绑定(IsFocused);
        focusedBinding.ElementName = tb.Name;
        dataTrigger.Value = TRUE;
        dataTrigger.Binding = focusedBinding;        二传手二传手=新二传手();
        setter.Property = Popup.IsOpenProperty;
        setter.Value = TRUE;
        dataTrigger.Setters.Add(二传);
        style.Triggers.Add(dataTrigger);        dataTrigger =新DataTrigger();
        focusedBinding =新的绑定(IsFocused);
        focusedBinding.ElementName = popupTextBox.Name;
        dataTrigger.Value = TRUE;
        dataTrigger.Binding = focusedBinding;
        二传手=新二传手();
        setter.Property = Popup.IsOpenProperty;
        setter.Value = TRUE;
        dataTrigger.Setters.Add(二传);
        style.Triggers.Add(dataTrigger);        popup.Style =风格;


解决方案

以下code证明其在StackPanel中两个文本框,设置重点顶端文本框将打开弹出。在这一点,如果你再移动焦点到包含在弹出的文本框中,将继续开放。如果将焦点移动到另一个元素,在这种情况下在StackPanel的第二个文本框,弹出式窗口将关闭。当你无法专注于自己弹出我其实在弹出绑定到文本框的IsFocused属性。

 <&StackPanel的GT;
    <文本框X:NAME =文字文本=这是一个文本框中输入/>
    <文本框的文本=另一个文本框/>
    <弹出PlacementTarget ={绑定的ElementName =文字}>
        < Popup.Style>
            <风格的TargetType ={X:类型弹出}>
                < Style.Triggers>
                    < D​​ataTrigger绑定={绑定的ElementName =文本,路径= IsFocused}VALUE =真>
                        < setter属性=ISOPENVALUE =真/>
                    < / DataTrigger>
                    < D​​ataTrigger绑定={绑定的ElementName = popupText,路径= IsFocused}VALUE =真>
                        < setter属性=ISOPENVALUE =真/>
                    < / DataTrigger>
                < /Style.Triggers>
            < /样式和GT;
        < /Popup.Style>
        <文本框X:NAME =popupText文本=HELLO WORLD/>
    < /弹出>
< / StackPanel的>

要实现在C#同样的事情,你没有使用的ElementName绑定,因为你已经具备的要素在手边。我几乎总是使用XAML来定义我的元素,所以我敢肯定,你可以整理这一点。

  VAR的text1 =新的TextBox {名称=文本,文本=这是一个文本框};
VAR文本2 =新文本框的{text =另一个文本框};
VAR popupText =新的TextBox {名称=popupText,文本=HELLO WORLD};
VAR弹出=新弹出{儿童= popupText,PlacementTarget =文本1};
VAR的StackPanel =新的StackPanel();stackPanel.Children.Add(文本1);
stackPanel.Children.Add(文本2);
stackPanel.Children.Add(弹出式);VAR popupStyle =新样式(typeof运算(弹出));
VAR textIsFocusedTrigger =新DataTrigger
    {
        结合新= {绑定源=文本1,路径=新的PropertyPath(IsFocused)},
        值=真
    };textIsFocusedTrigger.Setters.Add(新二传手(Popup.IsOpenProperty,真实));VAR popupTextIsFocusedTrigger =新DataTrigger
    {
        结合新= {绑定源= popupText,路径=新的PropertyPath(IsFocused)},
        值=真
    };popupTextIsFocusedTrigger.Setters.Add(新二传手(Popup.IsOpenProperty,真实));popupStyle.Triggers.Add(textIsFocusedTrigger);
popupStyle.Triggers.Add(popupTextIsFocusedTrigger);popup.Style = popupStyle;

我希望这有助于!

I have a TextBox and a Popup control. I want the Popup.IsOpen property to be bound to the TextBox.IsFocused property. In other words, if the textbox has focus, the popup is open. Alternatively, if the popup is in focus, I don't want it to close due to the textbox losing focus. I was hoping to handle this using bindings rather than having to deal with this in event handlers. Also, do I have to do anything regarding the dependency properties, since they are pre-existing(i.e. Register, override metadata, etc), or can I just bind to these properties.

Here is some sample code similar to my scenario

StackPanel sp = new StackPanel();
TextBox tb = new TextBox();
Popup popup = new Popup();

sp.Children.Add(tb);
sp.Children.Add(popup);
this.Content = sp;

Binding bd = new Binding("IsFocused");
bd.source = tb.IsFocused;
popup.SetBinding(Popup.IsOpenProperty, bd);

From this I was assuming that if I clicked on the textbox control and gave it focus, that the popup would open, and conversely if the textbox lost focus, that the popup would close. I can't seem to get this to work.

If someone has an idea of what I'm doing wrong, then maybe they could also answer the second half of my question that if the textbox loses focus but it was the popup that receives focus, that the popup will remain open or give focus back to the textbox so that it will remain open bc of the first binding. Any other control that gains focus when the textbox loses focus does not apply to this scenario.

If I could reword this for clarity I would say it like this.

1.) Bind Popup.IsOpen to TextBox.IsFocused

2.) Bind TextBox.IsFocused to Popup.IsFocused(assuming this will just give focus back to the textbox)


Here is my first C# attempt at this. Something is still not quite right. Nothing happens so I'm not quite sure where my mistake is.

        StackPanel sp = new StackPanel(); 
        TextBox tb = new TextBox(); 
        Popup popup = new Popup();

        TextBox popupTextBox = new TextBox();
        popup.Child = popupTextBox;


        sp.Children.Add(tb); 
        sp.Children.Add(popup); 
        this.Content = sp;


        //***Questions concerning giving the UIElement a name and registering it
        tb.Name = "siblingTextBox";
        System.Windows.NameScope.GetNameScope(tb).RegisterName("siblingTextBox", tb);

        //***Questions concerning giving the UIElement a name and registering it
        popupTextBox.Name = "popupTextBox";
        System.Windows.NameScope.GetNameScope(tb).RegisterName("popupTextBox", popupTextBox);

        Binding binding = new Binding();
        binding.ElementName = tb.Name;
        popup.PlacementTarget = tb;

        Style style = new Style();
        style.TargetType = typeof(Popup);

        DataTrigger dataTrigger = new DataTrigger();
        Binding focusedBinding = new Binding("IsFocused");
        focusedBinding.ElementName = tb.Name;
        dataTrigger.Value = true;
        dataTrigger.Binding = focusedBinding;

        Setter setter = new Setter();
        setter.Property = Popup.IsOpenProperty;
        setter.Value = true;
        dataTrigger.Setters.Add(setter);
        style.Triggers.Add(dataTrigger);

        dataTrigger = new DataTrigger();
        focusedBinding = new Binding("IsFocused");
        focusedBinding.ElementName = popupTextBox.Name;
        dataTrigger.Value = true;
        dataTrigger.Binding = focusedBinding;
        setter = new Setter();
        setter.Property = Popup.IsOpenProperty;
        setter.Value = true;
        dataTrigger.Setters.Add(setter);
        style.Triggers.Add(dataTrigger);

        popup.Style = style;

解决方案

The following code demonstrates having two text boxes in a StackPanel, setting focus to the top text box will open the Popup. At which point, if you then move Focus to the text box contained in the Popup it will remain open. If you move focus to another element, in this instance the second text box in the StackPanel, the Popup will close. As you unable to focus the Popup itself I am actually binding to the IsFocused property of the text box in the Popup.

<StackPanel>
    <TextBox x:Name="text" Text="This is a text box" />
    <TextBox Text="Another Text Box" />
    <Popup PlacementTarget="{Binding ElementName=text}">
        <Popup.Style>
            <Style TargetType="{x:Type Popup}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=text, Path=IsFocused}" Value="True">
                        <Setter Property="IsOpen" Value="True" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=popupText, Path=IsFocused}" Value="True">
                        <Setter Property="IsOpen" Value="True" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Popup.Style>
        <TextBox x:Name="popupText" Text="HELLO WORLD" />
    </Popup>
</StackPanel>

To achieve the same thing in C# you don't have to use the ElementName bindings as you already have the elements right at hand. I almost always use XAML to define my elements so I'm sure you could tidy this a bit.

var text1 = new TextBox { Name = "text", Text = "This is a text box" };
var text2 = new TextBox { Text = "Another Text Box" };
var popupText = new TextBox { Name = "popupText", Text = "HELLO WORLD" };
var popup = new Popup { Child = popupText, PlacementTarget = text1 };
var stackPanel = new StackPanel();

stackPanel.Children.Add(text1);
stackPanel.Children.Add(text2);
stackPanel.Children.Add(popup);

var popupStyle = new Style(typeof (Popup));
var textIsFocusedTrigger = new DataTrigger
    {
        Binding = new Binding {Source = text1, Path = new PropertyPath("IsFocused")},
        Value = true
    };

textIsFocusedTrigger.Setters.Add(new Setter(Popup.IsOpenProperty, true));

var popupTextIsFocusedTrigger = new DataTrigger
    {
        Binding = new Binding {Source = popupText, Path = new PropertyPath("IsFocused")},
        Value = true
    };

popupTextIsFocusedTrigger.Setters.Add(new Setter(Popup.IsOpenProperty, true));

popupStyle.Triggers.Add(textIsFocusedTrigger);
popupStyle.Triggers.Add(popupTextIsFocusedTrigger);

popup.Style = popupStyle;

I hope this helps!

这篇关于绑定文本框IsFocused弹出ISOPEN加上附加条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆