{"id":57001,"date":"2021-10-29T06:44:49","date_gmt":"2021-10-29T04:44:49","guid":{"rendered":"https:\/\/ar3dp.de\/docs\/programmieren\/wpf\/nuetzliche-bindings-fuer-wpf-anwendungen\/"},"modified":"2021-10-29T06:44:49","modified_gmt":"2021-10-29T04:44:49","slug":"nuetzliche-bindings-fuer-wpf-anwendungen","status":"publish","type":"docs","link":"https:\/\/ar3dp.de\/en\/docs\/programmieren\/wpf\/nuetzliche-bindings-fuer-wpf-anwendungen\/","title":{"rendered":"N\u00fctzliche Bindings f\u00fcr WPF-Anwendungen"},"content":{"rendered":"<h2 class=\"wp-block-heading\">General<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Grundlagen f\u00fcr Bindings unter XAML.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Bindings auf Eigenschaften (Properties) von Steuerelementen<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Folgendes Beispiel erstellt eine Bindung zu dem Steuerelement mit dem Namen &#8220;userMenu&#8221; auf die Eigenschaft &#8220;Background&#8221;. Somit ist gew\u00e4hrleistet, dass der Hintergrund des Untermen\u00fcs gleich dem Hintergrund des Hauptmen\u00fcs ist.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application\/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;XML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;xml&quot;}\">&lt;MenuItem\n    Background=&quot;{Binding ElementName=userMenu,Path=Background}&quot;\n \/&gt;<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application\/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;XML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;xml&quot;}\">&lt;MenuItem \n    x:Name=&quot;userMenu&quot;\n    DockPanel.Dock=&quot;Right&quot;\n    Background=&quot;#FF444444&quot;\n    Foreground=&quot;White&quot;\n    VerticalAlignment=&quot;Stretch&quot;\n    HorizontalAlignment=&quot;Stretch&quot;\n    Height=&quot;48&quot;\n    Header=&quot;Username&quot;\n    &gt; \n    &lt;MenuItem \n        x:Name=&quot;userMenuLogout&quot; \n        Background=&quot;{Binding ElementName=userMenu,Path=Background}&quot;\n        Foreground=&quot;{Binding ElementName=userMenu,Path=Foreground}&quot; \n        Header=&quot;Abmelden&quot;\n        &gt;\n    &lt;\/MenuItem&gt;\n&lt;\/MenuItem&gt;<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Binding auf statische Inhalte<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Manchmal kann es n\u00fctzlich sein, auf statische Inhalte zu verweisen.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application\/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;XML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;xml&quot;}\">xmlns:s=&quot;clr-namespace:System;assembly=mscorlib&quot;\n\n&lt;Label\n    Text=&quot;{x:Static s:Environment.UserName}&quot;\n\/&gt;<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Binding auf Settings.Default<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Wie du eine Bindung auf ein Settings-Element erstellen kannst, findest du im folgenden Beispiel.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application\/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;XML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;xml&quot;}\">xmlns:properties=&quot;clr-namespace:MyApp.Properties&quot;\n\n&lt;Label\n    Text=&quot;{Binding Source={x:Static properties:Settings.Default}, Path=NAME_OF_PROPERTY}&quot;\n\/&gt;<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Binding auf Eigenschaft vom aktuellen Steuerelement (Self)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Um eine Bindung auf eine Eigenschaft desselben Steuerelement zu erstellen, zeigt dir folgendes Beispiel.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application\/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;XML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;xml&quot;}\">&lt;Label\n    Text=&quot;{Binding RelativeSource={RelativeSource Self}, Path=Text}&quot;\n\/&gt;<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Binding auf Eigenschaft eines \u00fcbergeordneten Steuerelement (Parent)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Wie ein Child-Steuerelement auf eine Eigenschaft eines Parent-Steuerelementes zugreifen kann, siehst du folgend.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application\/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;XML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;xml&quot;}\">&lt;Label\n    Width=&quot;{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window, AncestorLevel=1}}&quot;\n\/&gt;<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Dabei gibt der AncestorType den Datentypen des zu suchenden, \u00fcbergeordneten Steuerelement. Das AncestorLevel gibt die Tiefe der Suche an. Bei &#8220;1&#8221; wird das erste Steuerelement des Typens &#8220;Window&#8221; verwendet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Eingabeelemente<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">TextBox<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">N\u00fctzliche Bindings f\u00fcr Textboxen.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Bei Enter n\u00e4chstes Element fokussieren<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Manchmal kann es hilfreich sein, wenn durch die &#8220;Enter&#8221;-Taste das n\u00e4chste Eingabeelement fokussiert wird. Dies ist mit wenig Code leicht realisierbar.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application\/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;XML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;xml&quot;}\">&lt;UserControl x:Class=&quot;Example.Views&quot;\n     xmlns=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&quot;\n     xmlns:x=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&quot;\n     xmlns:mc=&quot;http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006&quot; \n     xmlns:d=&quot;http:\/\/schemas.microsoft.com\/expression\/blend\/2008&quot; \n\n     xmlns:behaviors=&quot;http:\/\/schemas.microsoft.com\/xaml\/behaviors&quot;\n     \n     mc:Ignorable=&quot;d&quot; Loaded=&quot;UserControl_Loaded&quot; \n     &gt;\n    &lt;Grid&gt;\n    &lt;\/Grid&gt;\n&lt;\/UserControl&gt;<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application\/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;XML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;xml&quot;}\">&lt;!-- Verhalten f\u00fcr die jeweiligen Eingabeelemente --&gt;\n&lt;behaviors:Interaction.Triggers&gt;\n    &lt;behaviors:EventTrigger EventName=&quot;KeyDown&quot; &gt;\n        &lt;behaviors:InvokeCommandAction\n            Command=&quot;{Binding TextBoxKeyDownEventCommand}&quot;\n            PassEventArgsToCommand=&quot;True&quot;\n            \/&gt;\n    &lt;\/behaviors:EventTrigger&gt;\n&lt;\/behaviors:Interaction.Triggers&gt;<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;TrpContentRestriction&quot;:{&quot;restriction_type&quot;:&quot;exclude&quot;,&quot;selected_languages&quot;:[],&quot;panel_open&quot;:true},&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">public ICommand TextBoxKeyDownEventCommand { get =&gt; new RelayCommand((p) =&gt; TextBoxKeyDownEventAction(p)); }\nvoid TextBoxKeyDownEventAction(object args)\n{\n    try\n    {\n        if(args is KeyEventArgs keyArgs)\n        {\n            if(keyArgs.Key == Key.Enter)\n            {\n                if (keyArgs.Source is TextBox tb)\n                {\n                    keyArgs.Handled = true;\n                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next)\n                    {\n                        Wrapped = true\n                    };\n                    tb.MoveFocus(request);\n                }\n            }\n        }\n    }\n    catch (Exception exc)\n    {\n        \/\/handle any errors here\n    }\n}<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Allgemein Grundlagen f\u00fcr Bindings unter XAML. Bindings auf Eigenschaften (Properties) von Steuerelementen Folgendes Beispiel erstellt eine Bindung zu dem Steuerelement mit dem Namen &#8220;userMenu&#8221; auf die Eigenschaft &#8220;Background&#8221;. Somit ist gew\u00e4hrleistet, dass der Hintergrund des Untermen\u00fcs gleich dem Hintergrund des Hauptmen\u00fcs ist. Binding auf statische Inhalte Manchmal kann es n\u00fctzlich sein, auf statische Inhalte zu&#8230;<\/p>","protected":false},"author":1,"featured_media":0,"parent":54212,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_featured":false,"_is_vendor_doc":"0","footnotes":""},"doc_tag":[905,906,907,903],"class_list":["post-57001","docs","type-docs","status-publish","hentry","doc_tag-c","doc_tag-programming","doc_tag-visual-studio","doc_tag-wpf"],"comment_count":0,"_links":{"self":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/57001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/comments?post=57001"}],"version-history":[{"count":0,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/57001\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/54212"}],"next":[{"title":"DataGrid","link":"https:\/\/ar3dp.de\/en\/docs\/programmieren\/wpf\/datagrid\/","href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/docs\/57028"}],"wp:attachment":[{"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/media?parent=57001"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/ar3dp.de\/en\/wp-json\/wp\/v2\/doc_tag?post=57001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}