Reputation: 47
In the Window designer, I would make a group of RadioButtons to have the characteristics of a ToggleButton like this:
<StackPanel x:Name="ToolPanel" Orientation="Horizontal" Grid.Row="0">
<StackPanel.Resources>
<Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}"/>
</StackPanel.Resources>
<RadioButton x:Name="button_DrawNode" Click="Button_DrawNode_Checked" Content="Draw"/>
<RadioButton x:Name="button_Connect" Click="Button_Connect_Checked" Content="Connect"/>
<RadioButton x:Name="button_Move" Click="Button_Move_Checked" Content="Move"/>
<RadioButton x:Name="button_Remove" Click="Button_Remove_Checked" Content="Remove"/>
</StackPanel>
In code behind, I would create a StackPanel and then the subsequent RadioButtons like this:
StackPanel stackPanel = new StackPanel
{
Orientation = Orientation.Horizontal
};
Grid.SetRow(stackPanel, 0);
RadioButton button_DrawNode = new RadioButton { Content = "Draw" };
RadioButton button_Connect = new RadioButton { Content = "Connect" };
RadioButton button_Move = new RadioButton { Content = "Move" };
RadioButton button_Remove = new RadioButton { Content = "Remove" };
stackPanel.Children.Add(button_DrawNode);
stackPanel.Children.Add(button_Connect);
stackPanel.Children.Add(button_Move);
stackPanel.Children.Add(button_Remove);
However, scouring the Internet, I could not find an example of code behind to achieve the following tags:
<StackPanel.Resources>
<Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}"/>
</StackPanel.Resources>
To my understanding, putting a Style tag (like the one above) within the StackPanel.Resources tag will make all RadioButtons within the StackPanel have the characteristics of a ToggleButton, only needing to set it once.
In the following code, I can set the Style property for each RadioButton instance to make them behave as a ToggleButton, but I feel that would be unnecessarily repeating code.
Style toggleRadioButtonStyle = new Style(typeof(RadioButton), (Style)FindResource(typeof(ToggleButton)));
RadioButton button_DrawNode = new RadioButton { Content = "Draw", Style = toggleRadioButtonStyle };
RadioButton button_Connect = new RadioButton { Content = "Connect", Style = toggleRadioButtonStyle };
RadioButton button_Move = new RadioButton { Content = "Move", Style = toggleRadioButtonStyle };
RadioButton button_Remove = new RadioButton { Content = "Remove", Style = toggleRadioButtonStyle };
I tried to experiment with the following, thinking that the StackPanel's Style property set the StackPanel.Resources tag.
StackPanel stackPanel = new StackPanel
{
Orientation = Orientation.Horizontal,
Style = new Style(typeof(RadioButton), (Style)FindResource(typeof(ToggleButton)))
};
The result:
System.InvalidOperationException: ''RadioButton' TargetType does not match type of element 'StackPanel'.'
I concluded that the Style property of StackPanel is only to set it's own style.
I also looked at the Resources property of StackPanel but InteliSense says it only takes a ResourceDictionary.
Any help or insight will be greatly appreciated.
Upvotes: 0
Views: 178
Reputation: 3556
I think the title of this question is confusing and building visual elements in code behind is not generally recommended unless it is truly necessary.
That said, your mistake is obvious. You need to set the Style for RadioButton to the StackPanel's Resources, as you did in xaml, but not to the StackPanel's Style. To do so, Duke Cyrillus's answer to WPF Adding style resource without key in code behind is useful.
var stackPanel = new StackPanel { Orientation = Orientation.Horizontal };
stackPanel.Resources.Add(typeof(RadioButton), (Style)FindResource(typeof(ToggleButton)));
Upvotes: 0