コードからバインド添付挙動
-
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
}
}
私はあなたの他の質問やブログの記事を読みました。 ITSは、最終的にテキストボックスとは何の関係もありませんデフォルトボタンを実装するために、テキストボックスなどのさまざまなコントロールに奇妙な性質を添付する必要がありますする悪臭のように思えます。
デフォルト宣言しようとするボタンのプロパティを添付する(またはButton
呼ばDefaultButton
のサブクラスを作成するために)、その含むユーザーコントロール、ページとの同時動作持っているか、より良いパターンだろうキーを入力するために監視するChildWindow。これは、直接、この機能の提供に関連しない他のコントロールに関与する必要はありません。
所属していません StackOverflow