Bra*_*onS 3 wpf binding textbox dependency-properties focus
我有一个TextBox和一个Popup控件。我希望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);
Run Code Online (Sandbox Code Playgroud)
由此,我假设如果单击文本框控件并为其提供焦点,则弹出窗口将打开,反之,如果文本框失去焦点,则弹出窗口将关闭。我似乎无法让它发挥作用。
如果有人知道我做错了什么,那么也许他们也可以回答我问题的后半部分,即如果文本框失去焦点但弹出窗口接收焦点,则弹出窗口将保持打开状态或重新获得焦点到文本框,以便它在第一个绑定之后保持打开状态。当文本框失去焦点时获得焦点的任何其他控件不适用于此场景。
如果我可以为了清楚起见重新表述一下,我会这样说。
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;
Run Code Online (Sandbox Code Playgroud)
以下代码演示了 StackPanel 中有两个文本框,将焦点设置到顶部文本框将打开 Popup。此时,如果您将焦点移动到弹出窗口中包含的文本框,它将保持打开状态。如果将焦点移至另一个元素(在本例中为 StackPanel 中的第二个文本框),弹出窗口将关闭。由于您无法聚焦弹出窗口本身,我实际上绑定到弹出窗口中文本框的 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>
Run Code Online (Sandbox Code Playgroud)
要在 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;
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助!
| 归档时间: |
|
| 查看次数: |
13461 次 |
| 最近记录: |