Note that there are some explanatory texts on larger screens.

plurals
  1. POUserControl DataBinding with data conversion
    text
    copied!<p>I am relatively new to WPF and would like to create a UserControl that contains three RadioBoxes in a StackPanel. Each of the radio buttons has an IsChecked property. I want to ease access to that selection by publishing an integer property that indicates the current selection, call it SelectedIndex.</p> <p>So when the first radio is checked, the SelectedIndex value of my user control shall be 0; when the second radio is checked, SelectedIndex shall be 1, and so on. This should probably work in either direction: when the user clicks on the radio box, the SelectedIndex value shall change accordingly. And when the SelectedIndex value is changed by code or maybe even some data binding, the UI controls shall update accordingly.</p> <p>How can I do that with XAML code? I have the following code now:</p> <pre><code>public partial class CommandControl : UserControl { public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CommandControl)); public int SelectedIndex { get { return (int) GetValue(SelectedIndexProperty); } set { SetValue(SelectedIndexProperty, value); } } public CommandControl() { InitializeComponent(); } } </code></pre> <p>And the XAML part:</p> <pre><code>&lt;UserControl x:Class="MyNamespace.CommandControl" 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&gt; </code></pre> <p>I know how to pass values (like one of the RadioButton's Text for example) from a property of my UserControl. But this time, I need to map an "external" int value to three "internal" bool values, each true when the number has a specific value. I guess that should be done directly in XAML, but I cannot figure out how to accomplish that.</p> <p>Or would it be a better idea to write C# code for that conversion? I could possibly do that now, but I'm still unsure about the special DependencyProperty code which, I imagine, may change its value without passing my setter.</p>
 

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