绑定文本框IsFocused为Popup IsOpen以及其他条件

时间:2009-05-18 17:42:15

标签: wpf binding textbox focus dependency-properties

我有TextBoxPopup控件。我希望Popup.IsOpen属性绑定到TextBox.IsFocused属性。换句话说,如果文本框具有焦点,则弹出窗口是打开的。或者,如果弹出窗口处于焦点,我不希望它因文本框失去焦点而关闭。我希望使用绑定来处理这个问题,而不是在事件处理程序中处理这个问题。另外,我是否必须对依赖属性做任何事情,因为它们是预先存在的(即注册,覆盖元数据等),或者我可以只绑定到这些属性。

以下是一些类似于我的方案的示例代码

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);

由此我假设,如果我点击文本框控件并给予焦点,弹出窗口将打开,相反,如果文本框失去焦点,则弹出窗口将关闭。我似乎无法让这个工作。

如果有人知道我做错了什么,那么也许他们也可以回答我的问题的后半部分,如果文本框失去焦点但是弹出窗口会得到焦点,弹出窗口将保持打开或将焦点放回文本框,使其保持打开第一个绑定的bc。当文本框失去焦点时获得焦点的任何其他控件都不适用于此场景。

如果为了清楚起见我可以改写,我会这样说。

1。)Popup.IsOpen绑定到TextBox.IsFocused

2。)TextBox.IsFocused绑定到Popup.IsFocused(假设这只会将重点放回文本框)


这是我第一次尝试C#。有些东西还不太对劲。什么都没发生,所以我不太确定我的错误在哪里。

        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;

1 个答案:

答案 0 :(得分:3)

以下代码演示了在StackPanel中有两个文本框,将焦点设置到顶部文本框将打开Popup。此时,如果您将Focus移动到弹出窗口中包含的文本框,它将保持打开状态。如果将焦点移动到另一个元素,在本例中为StackPanel中的第二个文本框,则弹出窗口将关闭。由于您无法关注Popup本身,我实际上绑定到Popup中文本框的IsFocused属性。

<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>

要在C#中实现相同的功能,您不必使用ElementName绑定,因为您已经掌握了元素。我几乎总是使用XAML来定义我的元素,所以我相信你可以稍微整理一下。

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;

我希望这有帮助!