Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6879873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:54:07+00:00 2026-05-27T04:54:07+00:00

i have made a template that look like this : <ControlTemplate x:Key=onoffValue TargetType={x:Type Control}>

  • 0

i have made a template that look like this :

<ControlTemplate x:Key="onoffValue" TargetType="{x:Type Control}">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Height="20" Margin="0,5,0,0">
                <RadioButton Content="On" Height="20" Name="On_radiobutton" />
                <RadioButton Content="Off" Height="20" Name="Off_radiobutton" Margin="20,0,0,0" />
            </StackPanel>
   <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=BootSector}" Value="true">
                    <Setter TargetName="On_radiobutton" Property="IsChecked" Value="true"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=BootSector}" Value="false">
                    <Setter TargetName="Off_radiobutton" Property="IsChecked" Value="true"/>
                </DataTrigger>
   </ControlTemplate.Triggers>
</ControlTemplate>

For now, it is bind to the property BootSector(bool) ofa “Configuration” object.

I use this template in my window that has a configuration object as data context like this :

<Control Template="{StaticResource onoffValue}">

</Control>

It works great, but i want to go further.

I would like to know how i can pass a different property to my template to dynamically bind (dynamically change the property the template is bind to)
ie i tryed something like

<Control Template="{StaticResource onoffValue}" xmlns:test="{Binding Path=BootSector}"/>

and bind it in the template to “test” but it doesn’t work

Is it possible ? How can i do that ? I think i’m not too far away but not there still !

Thank you in advance

Edit : Concerning Dmitry answer :
There is a bug using that. When i do :

<StackPanel local:ToggleControl.IsOn="{Binding BootSector, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                Grid.Row="0" Grid.Column="1"
        Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                            <RadioButton Content="On" local:ToggleControl.Role="On"  Height="20" Margin="5" />
                            <RadioButton Content="Off" local:ToggleControl.Role="Off" Height="20" Margin="5" />
                        </StackPanel>  

By default BootSector is on false. When i click on the on button (true), it sets bootSector to true and then immediately to false . The behaviour should be that it stays to true until it is unchecked ? Is this related to the problem related here ? http://geekswithblogs.net/claraoscura/archive/2008/10/17/125901.aspx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:54:07+00:00Added an answer on May 27, 2026 at 4:54 am

    Here, the idea is – generic behaviors are never complex and generally not worth creating a custom control. I undertand that implmentation may vary, but the approach will remain the same. It makes sense to use XAML for the parts which can change and code for the stuff which will remain constant.

    UPDATE 1- It’s getting even easier when using Custom controls. You won’t need attached property no more – as you’ll get a dedicated space for it inside your custom control, also, you can use x:Name and GetTemplateChild(..) to otain a reference to individual RadioButtons.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    
    namespace RadioButtons
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                this.Loaded += (o, e) =>
                {
                    this.DataContext = new TwoBoolean()
                    {
                        PropertyA = false,
                        PropertyB = true
                    };
                };
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show(((TwoBoolean)this.DataContext).ToString());
            }
        }
    
        public enum RadioButtonRole
        { 
            On,
            Off
        }
    
    
        public class ToggleControl : DependencyObject
        {
            public static readonly DependencyProperty IsOnProperty =
                DependencyProperty.RegisterAttached("IsOn",
                typeof(bool?),
                typeof(ToggleControl),
                new PropertyMetadata(null, 
                    new PropertyChangedCallback((o, e) => 
                    {
                        ToggleControl.OnIsOnChanged((Panel)o, (bool)e.NewValue);
                    })));
    
    
            public static readonly DependencyProperty RoleProperty =
                DependencyProperty.RegisterAttached("Role",
                typeof(RadioButtonRole?),
                typeof(ToggleControl),
                new PropertyMetadata(null,
                    new PropertyChangedCallback((o, e) =>
                    {
    
                    })));
    
            private static readonly DependencyProperty IsSetUpProperty =
                DependencyProperty.RegisterAttached("IsSetUp",
                typeof(bool),
                typeof(ToggleControl),
                new PropertyMetadata(false));
    
            private static void OnIsOnChanged(Panel panel, bool e)
            {
                if (!ToggleControl.IsSetup(panel))
                {
                    ToggleControl.Setup(panel);
                }
    
                RadioButtonRole role;
    
                if (e)
                {
                    role = RadioButtonRole.On;
                }
                else
                {
                    role = RadioButtonRole.Off;
                }
    
                ToggleControl.GetRadioButtonByRole(role, panel).IsChecked = true;
            }
    
            private static void Setup(Panel panel)
            {
                // get buttons
                foreach (RadioButton radioButton in
                    new RadioButtonRole[2]
                    {
                        RadioButtonRole.On, 
                        RadioButtonRole.Off
                    }.Select(t =>
                        ToggleControl.GetRadioButtonByRole(t, panel)))
                {
                    radioButton.Checked += (o2, e2) => 
                    {
                        RadioButton checkedRadioButton = (RadioButton)o2;
    
                        panel.SetValue(ToggleControl.IsOnProperty, 
                            ToggleControl.GetRadioButtonRole(checkedRadioButton) == RadioButtonRole.On);
                    };
                }
    
                panel.SetValue(ToggleControl.IsSetUpProperty, true);
            }
    
            private static bool IsSetup(Panel o)
            {
                return (bool)o.GetValue(ToggleControl.IsSetUpProperty);
            }
    
            private static RadioButton GetRadioButtonByRole(RadioButtonRole role,
                Panel container)
            {
                return container.Children.OfType<RadioButton>().First(t => 
                    (RadioButtonRole)t.GetValue(ToggleControl.RoleProperty) == role);
            }
    
            private static RadioButtonRole GetRadioButtonRole(RadioButton radioButton)
            {
                return (RadioButtonRole)radioButton.GetValue(ToggleControl.RoleProperty);
            }
    
            public static void SetIsOn(DependencyObject o, bool? e)
            {
                o.SetValue(ToggleControl.IsOnProperty, e);
            }
    
            public static bool? GetIsOn(DependencyObject e)
            {
                return (bool?)e.GetValue(ToggleControl.IsOnProperty);
            }
    
            public static void SetRole(DependencyObject o, RadioButtonRole? e)
            {
                o.SetValue(ToggleControl.RoleProperty, e);
            }
    
            public static RadioButtonRole? GetRole(DependencyObject e)
            {
                return (RadioButtonRole?)e.GetValue(ToggleControl.RoleProperty);
            }
        }
    
        public class TwoBoolean: INotifyPropertyChanged
        {
            private bool propertyA, propertyB;
    
            public bool PropertyA
            {
                get
                {
                    return this.propertyA;
                }
                set
                {
                    this.propertyA = value;
    
                    this.OnPropertyChanged("PropertyA");
                }
            }
    
            public bool PropertyB
            {
                get
                {
                    return this.propertyB;
                }
                set
                {
                    this.propertyB = value;
    
                    this.OnPropertyChanged("PropertyB");
                }
            }
    
            private void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, 
                        new PropertyChangedEventArgs(propertyName));
                }
            }
    
            public override string ToString()
            {
                return string.Format("PropertyA:{0}, PropertyB:{1}", this.PropertyA, this.PropertyB);
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
    }
    

    Markup:

    <Window x:Class="RadioButtons.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:RadioButtons"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Margin="5" VerticalAlignment="Center">PropertyA</TextBlock>
            <StackPanel local:ToggleControl.IsOn="{Binding PropertyA, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                        Grid.Row="0" Grid.Column="1"
                Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <RadioButton Content="On" local:ToggleControl.Role="On"  Height="20" Margin="5" />
                <RadioButton Content="Off" local:ToggleControl.Role="Off" Height="20" Margin="5" />
            </StackPanel>
    
            <TextBlock Grid.Row="1" Grid.Column="0" Margin="5" VerticalAlignment="Center">PropertyB</TextBlock>
            <StackPanel local:ToggleControl.IsOn="{Binding PropertyB, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                        Grid.Row="1" Grid.Column="1"
                Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <RadioButton Content="On" local:ToggleControl.Role="On"  Height="20"  Margin="5" />
                <RadioButton Content="Off" local:ToggleControl.Role="Off" Height="20" Margin="5" />
            </StackPanel>
            <Button Click="Button_Click" Grid.Row="3" Grid.ColumnSpan="2">Save</Button>
        </Grid>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pre-made Word Template that has a table. I would like to
I have a template I made that sets variables that rarely change, call my
This scoping problem seems like the type of C++ quandary that Scott Meyers would
In one of our project, we would like to have a calendar control that
I have a template class that I have made called hash . My template
I have a bunch of legacy documents that are HTML-like. As in, they look
I have a wordpress blog. I made a custom page-template that allows users to
I have made a SVG image, or more like mini application, for viewing graphs
I have made a user control in asp.net c#. Becuase of some data I
I have made a simple HTML for my ebay template and I wanted to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.