<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lars Hildebrandt's Blog &#187; RadioButton</title>
	<atom:link href="http://blog.larshildebrandt.de/tag/radiobutton/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.larshildebrandt.de</link>
	<description>übers Programmieren und mehr...</description>
	<lastBuildDate>Mon, 28 Nov 2011 21:11:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Binding eines Enums an mehrere RadioButtons</title>
		<link>http://blog.larshildebrandt.de/binding-eines-enums-an-mehrere-radiobuttons/14.html</link>
		<comments>http://blog.larshildebrandt.de/binding-eines-enums-an-mehrere-radiobuttons/14.html#comments</comments>
		<pubDate>Sat, 01 Mar 2008 17:28:40 +0000</pubDate>
		<dc:creator>Lars Hildebrandt</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programmieren]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[Enum]]></category>
		<category><![CDATA[RadioButton]]></category>
		<category><![CDATA[ValueConverter]]></category>

		<guid isPermaLink="false">http://blog.larshildebrandt.de/2008/03/01/binding-eines-enums-an-mehrere-radiobuttons/</guid>
		<description><![CDATA[Bei einem meiner WPF-Projekte ergab sich die Problematik, das ich ein Enum an mehrere RadioButtons binden wollte. Nach recht langer Suche und einigen Versuchen habe ich einen Lösungsweg gefunden, den ich hier an einem kleinen Beispiel zeigen will. Den Source für den ValueConverter habe ich mir nicht ausgedacht. Beim googlen nach der Lösung habe ich [...]]]></description>
			<content:encoded><![CDATA[<p>Bei einem meiner WPF-Projekte ergab sich die Problematik, das ich ein Enum an mehrere RadioButtons binden wollte. Nach recht langer Suche und einigen Versuchen habe ich einen Lösungsweg gefunden, den ich hier an einem kleinen Beispiel zeigen will. Den Source für den ValueConverter habe ich mir nicht ausgedacht. Beim googlen nach der Lösung habe ich durch Zufall diese <a href="http://blogs.wankuma.com/naka/archive/2007/09/16/96518.aspx" target="blank">Seite</a> gefunden und den darauf befindlichen Source verwendet.</p>
<p>Ausgangspunkt ist einen kleine Klasse und ein Enum</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">enum</span> ProjectStatus 
<span style="color: #008000;">&#123;</span>
    InVorbereitung,
    InArbeit,
    Abgeschlossen
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Project
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> ProjectName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> ProjectStatus Status <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><span id="more-14"></span><br />
Nun soll diese Klasse per Binding in einem WPF-Window dargestellt werden.<br />
Dafür erstellen wir ein Exemplar der Klasse im Constructor des Windows und weise es dem DataContext zu:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Project project <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Project<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
project<span style="color: #008000;">.</span><span style="color: #0000FF;">ProjectName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;RadioButton Binding Test&quot;</span><span style="color: #008000;">;</span>
project<span style="color: #008000;">.</span><span style="color: #0000FF;">Status</span> <span style="color: #008000;">=</span> ProjectStatus<span style="color: #008000;">.</span><span style="color: #0000FF;">InArbeit</span><span style="color: #008000;">;</span>  
DataContext <span style="color: #008000;">=</span> project<span style="color: #008000;">;</span></pre></div></div>

<p>Und nun kann im Xaml alles entsprechend gebunden werden. Dabei sollen alle möglichen Enum-Werte als RadioButtons dargestellt werden. Hier das Formular ohne Werte: <img src='http://blog.larshildebrandt.de/wp-content/uploads/2008/03/20080301_radiobuttonbinding_empty.jpg' alt='Das leere Formular' /><br />
Hier der vollständige Xaml-Code</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;WPFRadioButtonBinding.frmMain&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:c</span>=<span style="color: #ff0000;">&quot;clr-namespace:WPFRadioButtonBinding&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;Binding&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;150&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;250&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:EnumBooleanConverter</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;enumBooleanConverter&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;2,0,0,0&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;lblProjectName&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;ProjectName&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;2,0,0,0&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;txtProjectName&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding ProjectName}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RadioButton</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;radInVorbereitung&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;InVorbereitung&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">IsChecked</span>=<span style="color: #ff0000;">&quot;{Binding Path=Status, Converter={StaticResource enumBooleanConverter}, ConverterParameter=InVorbereitung}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RadioButton</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;radInArbeit&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;InArbeit&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">IsChecked</span>=<span style="color: #ff0000;">&quot;{Binding Path=Status, Converter={StaticResource enumBooleanConverter}, ConverterParameter=InArbeit}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RadioButton</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;radAbgeschlossen&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Abgeschlossen&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">IsChecked</span>=<span style="color: #ff0000;">&quot;{Binding Path=Status, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Abgeschlossen}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;txtStatus&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Bottom&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Status}&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Der Trick ist beim Binding des Enums an die RadioButtons. Hierfür wird ein ValueConverter verwendet, der per Parameter &#8220;gesagt&#8221; bekommt, um welchen Wert es sich handet. Der ValueConverter &#8220;entscheidet dann, on es sich um den richtigen Wert handelt oder auch nicht&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">IsChecked=&quot;{Binding Path=Status, Converter={StaticResource enumBooleanConverter}, ConverterParameter=InVorbereitung}&quot;/&gt;</pre></div></div>

<p>Hier noch der Source für den ValueConverter:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> EnumBooleanConverter <span style="color: #008000;">:</span> IValueConverter
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080;">#region IValueConverter Members</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Convert<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInfo</span> culture<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> ParameterString <span style="color: #008000;">=</span> parameter <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ParameterString <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">UnsetValue</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsDefined</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, value<span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">UnsetValue</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">object</span> paramvalue <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, ParameterString<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>paramvalue<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> ConvertBack<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInfo</span> culture<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> ParameterString <span style="color: #008000;">=</span> parameter <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ParameterString <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">UnsetValue</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>targetType, ParameterString<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> 
    <span style="color: #008080;">#endregion</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Hier der vollständige Source (Visual Studio 2008): <a href='http://blog.larshildebrandt.de/wp-content/uploads/2008/03/20080301_wpfradiobuttonbinding.zip' title='wpfradiobuttonbinding.zip'>Download</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.larshildebrandt.de/binding-eines-enums-an-mehrere-radiobuttons/14.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

