{"id":130,"date":"2016-03-03T18:10:55","date_gmt":"2016-03-03T17:10:55","guid":{"rendered":"http:\/\/dtp-soft.de\/?p=130"},"modified":"2016-03-03T13:12:04","modified_gmt":"2016-03-03T12:12:04","slug":"wpf-binding-an-properties","status":"publish","type":"post","link":"http:\/\/dtp-soft.de\/?p=130","title":{"rendered":"WPF: Binding an Properties"},"content":{"rendered":"<p>Damit die Daten auf der Oberfl\u00e4che immer aktuell sind, sollte man sie per Binding an die entsprechenden Properties kn\u00fcpfen.<\/p>\n<p>Das ist im einfachsten Fall einfach ein<\/p>\n<pre class=\"lang:xhtml decode:true\" title=\"einfache Bindung\">Text=\"{Binding Source=StaticRecource TMyClass},Path=MyProperty,Mode=TwoWay}\"<\/pre>\n<p>Wenn es allerdings um RadioButtons geht, muss man oft einen Enum-Wert in einen boolschen Ausdruck umsetzen. Daf\u00fcr gibt es zum Gl\u00fcck ebenfalls einen relativ einfache L\u00f6sung. Angenommen, der Code sieht etwa so aus:<\/p>\n<pre class=\"lang:c# decode:true\" title=\"Beispielklasse\">namespace MyNS\r\n{\r\n    public enum MyEnum\r\n    {\r\n        OptionA,\r\n        OptionB,\r\n        OptionC\r\n    }\r\n\r\n    public class MyClass\r\n    {\r\n        public MyEnum MyOption\r\n        {\r\n            get { return (MyEnum)GetValue(MyOptionProperty); }\r\n            set { SetValue(OperationModeProperty, value); }\r\n        }\r\n\r\n        \/\/ Using a DependencyProperty as the backing store for OperationMode. This enables animation, styling, binding, etc...\r\n        public static readonly DependencyProperty MyOptionProperty =\r\n              DependencyProperty.Register(\"MyOption\", typeof(MyEnum), typeof(MyClass), new PropertyMetadata(MyEnum.OptionB)); \r\n\r\n        public MyClass()\r\n        {\r\n        }\r\n    }\r\n}<\/pre>\n<p>Dann f\u00fcgt man folgenden Konverter hinzu:<\/p>\n<pre class=\"lang:default decode:true\" title=\"Konverter f\u00fcr RadioButton IsChecked\">public class RadioButtonCheckedConverter : System.Windows.Data.IValueConverter\r\n{\r\n    public object Convert(object value, Type targetType, object parameter,\r\n\t\t\tSystem.Globalization.CultureInfo culture)\r\n    {\r\n        return value.Equals(parameter);\r\n    }\r\n\r\n    public object ConvertBack(object value, Type targetType, object parameter,\r\n\t\t\tSystem.Globalization.CultureInfo culture)\r\n    {\r\n        return value.Equals(true) ? parameter : System.Windows.Data.Binding.DoNothing;\r\n    }\r\n}<\/pre>\n<p>Nun kann man den Konverter in dem Xaml bekannt machen<\/p>\n<pre class=\"lang:xhtml decode:true\" title=\"Bekannt machen der Klasse und des Konverters\">&lt;Window ...\r\n    xmlns:src=\"clr-namespace:MyNS\"\r\n    &gt;\r\n&lt;Window.Resources&gt;\r\n    ...\r\n    &lt;src:RadioButtonCheckedConverter x:Key=\"RadioButtonCheckedConverter\" \/&gt;\r\n    &lt;src:MyClass x:Key=\"TMyClass\" \/&gt;\r\n&lt;\/Window.Resources&gt;<\/pre>\n<p>und dann f\u00fcr den RadioButton verwenden:<\/p>\n<pre class=\"wrap:true lang:xhtml decode:true\" title=\"Binden der IsChecked Eigenschaft eines RadioButtons an eine Enum\">&lt;RadioButton GroupName=\"MyGroup\" \r\n             Content=\"Option A\" \r\n             IsChecked=\"{Binding Source={StaticResource TMyClass},Path=MyOption,Mode=TwoWay,Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static t:MyEnum.OptionA}}\"\/&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Vollst\u00e4ndiges Beispiel zur obigen Klasse:<\/p>\n<pre class=\"wrap:true lang:xhtml decode:true\" title=\"komplettes Beispiel\">&lt;Window x:Class=\"MyNS.MyClass\"\r\n        xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\r\n        xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\r\n\txmlns:src=\"clr-namespace:LVRepair\"\r\n        Title=\"MyTitle\"&gt;\r\n\t&lt;Window.Resources&gt;\r\n\t\t&lt;src:MyClassr x:Key=\"TMyClass\"\/&gt;\r\n\t\t&lt;src:RadioButtonCheckedConverter x:Key=\"RadioButtonCheckedConverter\" \/&gt;\r\n\t&lt;\/Window.Resources&gt;\r\n\t&lt;Grid&gt;\r\n\t\t&lt;Grid.RowDefinitions&gt;\r\n\t\t\t&lt;RowDefinition Height=\"*\"\/&gt;\r\n\t\t\t&lt;RowDefinition Height=\"*\"\/&gt;\r\n\t\t\t&lt;RowDefinition Height=\"*\"\/&gt;\r\n\t\t&lt;\/Grid.RowDefinitions&gt;\r\n\t\t&lt;RadioButton GroupName=\"Option\" \r\n\t\t\t     Content=\"Option A\" \r\n\t\t\t     IsChecked=\"{Binding Source={StaticResource TMyClass},Path=MyOption,Mode=TwoWay,Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static src:MyEnum.OptionA}}\"\/&gt;\r\n\t\t&lt;RadioButton GroupName=\"Option\" \r\n\t\t\t     Content=\"Option B\" \r\n\t\t\t     IsChecked=\"{Binding Source={StaticResource TMyClass},Path=MyOption,Mode=TwoWay,Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static src:MyEnum.OptionB}}\"\/&gt;\r\n\t\t&lt;RadioButton GroupName=\"Option\" \r\n\t\t\t     Content=\"Option C\" \r\n\t\t\t     IsChecked=\"{Binding Source={StaticResource TMyClass},Path=MyOption,Mode=TwoWay,Converter={StaticResource RadioButtonCheckedConverter}, ConverterParameter={x:Static src:MyEnum.OptionC}}\"\/&gt;\r\n\t&lt;\/Grid&gt;\r\n&lt;\/Windows&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Damit die Daten auf der Oberfl\u00e4che immer aktuell sind, sollte man sie per Binding an die entsprechenden Properties kn\u00fcpfen. Das ist im einfachsten Fall einfach ein Text=&#8220;{Binding Source=StaticRecource TMyClass},Path=MyProperty,Mode=TwoWay}&#8220; Wenn es allerdings um RadioButtons geht, muss man oft einen Enum-Wert in einen boolschen Ausdruck umsetzen. Daf\u00fcr gibt es zum Gl\u00fcck ebenfalls einen relativ einfache L\u00f6sung.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1,26],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-allgemein","category-wpf"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/dtp-soft.de\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/dtp-soft.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dtp-soft.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dtp-soft.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/dtp-soft.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":8,"href":"http:\/\/dtp-soft.de\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":138,"href":"http:\/\/dtp-soft.de\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions\/138"}],"wp:attachment":[{"href":"http:\/\/dtp-soft.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dtp-soft.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dtp-soft.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}