结合从代码中附加的行为
-
27-09-2019 - |
题
我使用的一个自定义的默认按钮附加行为。(如本文所定义: Silverlight 4的默认按钮服务)
我能够在XAML成功绑定这一点,但在一个嵌套的用户控制,在下文中后面的代码不工作:
public partial class MyNestedUserContol{
/// a DP for storing the name of the default button, used in the ElementName binding
public string DefaultButton {
get { return (string)GetValue(DefaultButtonProperty); }
set { SetValue(DefaultButtonProperty, value); }
}
public static readonly DependencyProperty DefaultButtonProperty =
DependencyProperty.Register("DefaultButton", typeof(string), typeof(TeamReferenceFieldEditor), new PropertyMetadata(null));
...
private void CreateCustomControls(){
...
TextBox tb = new TextBox();
...
AddDefaultButtonBinding(tb);
...
}
private void AddDefaultButtonBinding(Control control) {
Binding binding = new Binding();
binding.ElementName = this.DefaultButton;
control.SetBinding(DefaultButtonService.DefaultButtonProperty, binding); }
...
}
我应该如何创建该代码绑定?点击 谢谢你,结果 标记
解决方案
最终,这将是在名称范围的问题。分配给的ElementName的值可能不以相同的名称范围,该文本框属于存在。
你有没有考虑简单地使用: -
private void MainPage_Load(object sender, RoutedEventArgs e)
{
LayoutRoot.AddHandler(UIElement.KeyUpEvent, LayoutRoot_KeyUp, true)
}
private void LayoutRoot_Keyup(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// invoke default operation here
}
}
我已经通过您的其他问题和博客文章阅读。它似乎是一个不好的气味有附加怪属性的各种控件像一个文本框来实现一个默认的按钮,这最终有无关的文本。
与含用户控件,页或这将是更好的图案附着在按钮的属性将被声明的默认(或创建一个子类Button
称为DefaultButton
的),并以具有协同操作ChildWindow观看了回车键。这将不会再有涉及不直接相关的递送该功能的其他控件。
不隶属于 StackOverflow