Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The most common solution is not to attempt to use a numerical representation of which <code>RadioButton</code> is selected because you're going against the flow if you try.</p> <p>However, to try and answer the question nonetheless...</p> <p>I don't think I'd try to solve this in XAML, because this isn't playing to XAML's strengths. Something people relatively new to WPF often do is to try and do everything possible in XAML whether it makes sense or not because they've got the idea from somewhere that it's what you're supposed to do.</p> <p>I'm not saying it's impossible. Lest you think I'm trying to talk you out of it because I don't know how to do it in XAML, I'll show exactly how I think you shouldn't do it. This works, for small values of "works":</p> <pre><code>&lt;UserControl x:Class="MyNamespace.CommandControl" xmlns:loc="clr-namespace:MyNamespace" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="65" d:DesignWidth="219" x:Name="me"&gt; &lt;StackPanel HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Center" Orientation="Horizontal"&gt; &lt;RadioButton Content="Disable" Name="disableRadio" VerticalAlignment="Center" IsChecked="True" /&gt; &lt;RadioButton Content="Enable" Name="enableRadio" VerticalAlignment="Center" Margin="5,0,0,0" /&gt; &lt;RadioButton Content="Configure" Name="configureRadio" VerticalAlignment="Center" Margin="5,0,0,0" /&gt; &lt;/StackPanel&gt; &lt;UserControl.Style&gt; &lt;Style TargetType="loc:MyNamespace"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding ElementName=disableRadio, Path=IsChecked}" Value="True"&gt; &lt;DataTrigger.Setters&gt; &lt;Setter Property="SelectedIndex" Value="0" /&gt; &lt;/DataTrigger.Setters&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding ElementName=enableRadio, Path=IsChecked}" Value="True"&gt; &lt;DataTrigger.Setters&gt; &lt;Setter Property="SelectedIndex" Value="1" /&gt; &lt;/DataTrigger.Setters&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding ElementName=configureRadio, Path=IsChecked}" Value="True"&gt; &lt;DataTrigger.Setters&gt; &lt;Setter Property="SelectedIndex" Value="2" /&gt; &lt;/DataTrigger.Setters&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/UserControl.Style&gt; &lt;/UserControl&gt; </code></pre> <p>but this is pretty icky. First of all, Visual Studio will pretend there's an error - for as long as you have this Xaml file open it will report the error "'CommandControl' TargetType does not match type of element 'UserControl'." Secondly, this only works if you put the <code>Style</code> after the main body of the user control. Frankly these sorts of issues are code smells - a clue that you may be doing things wrong. And in this case, you would be.</p> <p>By the way, Visaul Studio's not telling the truth with the error message - that's not a compile-time error, and the program will build and run, and using the following main window content:</p> <pre><code>&lt;Grid&gt; &lt;my:CommandControl HorizontalAlignment="Left" Margin="224,114,0,0" x:Name="commandControl1" VerticalAlignment="Top" /&gt; &lt;TextBlock Height="82" HorizontalAlignment="Left" Margin="76,162,0,0" Name="textBlock1" Text="{Binding ElementName=commandControl1, Path=SelectedIndex}" VerticalAlignment="Top" Width="237" /&gt; &lt;/Grid&gt; </code></pre> <p>I've verified that this does correctly update the property.</p> <p>But this is all wrong.</p> <p>For one thing, what you're trying to create here is programatic functionality - you're determining how the control's API behaves. This is well outside XAML's strengths. Heck, even embedding interactive behaviour in XAML is questionable. My view is that XAML is mainly for the layout and appearance of your app.</p> <p>So the codebehind is really the place for what you're doing. Just use good old-fashioned event handling. Be sure to use <code>SetCurrentValue</code> to update your depenedency property's value. Don't just use the setter, because that will mess up any data bindings or other DP-specific uses of your property, and will essentially defeat the point of making it a DP in the first place.</p> <p>Also, I wouldn't call it <code>SelectedIndex</code> because that's a property name that usually means something specific in WPF - it's mainly used by ItemsControl-derived controls that support selection. That's not what you're doing here, so I'd be inclined to pick a name that doesn't already commonly have a meaning. Except as I already said, I don't think I'd be using a numeric index in any case.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload