How to get the value of the checked radiobutton in wpf - c#

I have four RadioButtons in a grid panel, but when I do this:
<GroupBox x:Name="radioButtons">
<RadioButton Content="1" Height="16" HorizontalAlignment="Left" Margin="10,45,0,0" Name="status1" VerticalAlignment="Top" />
<RadioButton Content="2" Height="16" HorizontalAlignment="Left" Margin="10,67,0,0" Name="status2" VerticalAlignment="Top" />
<RadioButton Content="3" Height="16" HorizontalAlignment="Left" Margin="10,89,0,0" Name="status3" VerticalAlignment="Top" />
<RadioButton Content="4" Height="16" HorizontalAlignment="Left" Margin="10,111,0,0" Name="status4" VerticalAlignment="Top" />
</GroupBox>
It says that:
Error 1 The object 'GroupBox' already has a child and cannot add 'RadioButton'. 'GroupBox' can accept only one child.
And the last three RadioButtons say:
The property 'Content' is set more than once.
What's wrong with my GroupBox? Furthermore, in my code I want to access the RadioButton that is checked (preferably as an int). How do I do this? I tried to look in Google and I found a lot of results, but I couldn't understand any of them.

GroupBox can only hold 1 item, hence the error about trying to set the Content property of the GroupBox multiple times.
So, make that a Layout item and then put the RadioButtons inside it. Now, you set the Content once, which is the StackPanel, and that Layout item can hold many children -> RadioButtons.
<GroupBox x:Name="radioButtons">
<StackPanel>
<RadioButton Name="status1"
Height="16"
Margin="10,45,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="1" />
<RadioButton Name="status2"
Height="16"
Margin="10,67,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="2" />
<RadioButton Name="status3"
Height="16"
Margin="10,89,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="3" />
<RadioButton Name="status4"
Height="16"
Margin="10,111,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="4" />
</StackPanel>
</GroupBox>
As for your second question, Christian Mosers WPF Tutorial.net has a decent sample. If you don't understand it, you maybe should look at the topics Binding and Converter, first.
A very crude way to be notified of RadioButton checked in a non MVVM way:
private void RadioButtonChecked(object sender, RoutedEventArgs e) {
var radioButton = sender as RadioButton;
if (radioButton == null)
return;
int intIndex = Convert.ToInt32(radioButton.Content.ToString());
MessageBox.Show(intIndex.ToString(CultureInfo.InvariantCulture));
}
Then, in each of your RadioButtons in xaml, add Checked="RadioButtonChecked".

If you want many elements or controls then you have to put them in layout containers.
Grid
StackPanel
DockPanel
WrapPanel
Etc.....

Related

How to use click event in MaterialDesign:PackIcon in WPF?

I used materialDesign:PackIkon in my WPF application.
this is my code at xaml for the PackIcon
<ListViewItem Background="White" Height="55" >
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="CardMembership" MouseDown="PackIconMember_MouseDown" Height="40" Width="25" Foreground="#FF0959A8" />
<Button x:Name="btnMember" Click="btnMember_Click" Content="Member" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" FontSize="10" />
</StackPanel>
</ListViewItem>
I have a few PackIcon in my xaml. each of them in different listview.
what I want is when user click on the Icon it will process the event in PackIconMember_MouseDown
There is no error in my code above, the problem is sometimes the code work. I means when user click on the icon it will process the event. but sometimes user need to click multiple time for it to process the event. I don't know why this happen.
Any idea on what I should do with this ? or any suggestion to replace the MouseDown event.
its only work when I click on icon with the blue color.
it does not working when I click on the white space that I show with the arrow. how can I do to make it work when user click anywhere on the icon ? is it possible ?
if I do inside button, the packIcon does not appear
<Button Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" Height="20" Width="25" Margin="10">
<materialDesign:PackIcon MouseDown="PackIconMember_MouseDown" TouchDown="PackIconMember_MouseDown" Kind="CardMembership" />
</Button>
Set the Background property of the PackIcon to Transparent:
<materialDesign:PackIcon Kind="CardMembership" MouseDown="PackIconMember_MouseDown" Height="40" Width="25" Foreground="#FF0959A8"
Background="Transparent" />
This should capture the clicks also on the "empty" parts of the icon.
Make The Icon as Part of the Button Like:
<Button x:Name="btnMember" Click="btnMember_Click" Content="Member" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" FontSize="10" >
<materialDesign:PackIcon Kind="CardMembership" Height="40" Width="25" Foreground="#FF0959A8" />
</Button
But you have to set the margin

How can get the value of Radio Button Inside Listbox Click on Button in Windows Phone Using C#

I have Radio Group of Three Radio Button and a submit button also
inside of ListBox. I want to get the selected Radio Button Value from
the Radio Group on Button Click. Below my listbox item view. please
help me.
<ListBox x:Name="OptionListView" SelectionChanged="OptionListView_SelectionChanged" Height="720" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="#99000000" Width="480" Margin="0,10,0,0">
<StackPanel Margin="10">
<TextBlock Text="{Binding FeaturedQuestion}" TextWrapping="Wrap" />
<RadioButton Content="Yes" GroupName="OpinionPoll" Foreground="White"/>
<RadioButton Content="No" GroupName="OpinionPoll" Foreground="White"/>
<RadioButton Content="Not Decide" GroupName="OpinionPoll" Foreground="White"/>
<Button x:Name="btnSubmit" Click="btnSubmit_Click" Background="#d44740" Content="Submit" Style="{StaticResource TileButtonTemplate}" Height="65" VerticalAlignment="Top" HorizontalAlignment="Left" Width="200" Foreground="White"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When you work with a listbox you need to have a List of something like a object.
This object represent your 3 radio button. For exemple you can work with an enum for get the checked value or create a custom radio button with 3 states.
Steps:
- understand how to use and bind control inside listbox
understand how to get the checked value (Exemple => IsChecked="{Binding IsSelected, Mode=TwoWay}"/>)
You could simply access the properties, by binding the ListBox's selected item.
Reference: how do I access a radiobutton inside a ListBoxItem in windows phone 7

Placing more than one element into a GroupBox in WPF

I'm trying to make a GroupBox with a few (radio)buttons in it. But in the samples i'm using checkbox.
<GroupBox Header="Aðgerðir" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="270,0,0,178" Height="106" Width="176">
<CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
</GroupBox>
The above does not work and visual studio says "Invalid Markup".
This here works fine though
<GroupBox Header="Aðgerðir" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="270,0,0,178" Height="106" Width="176">
<CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</GroupBox>
I don't get it. It says "Content can only be set once" if i run the debugger, but erasing the Content part of the checkbox seems to have no effect.
The content of the GroupBox can only be set once, meaning that it can only have a single control inside it. If you want to two radio button, place them in a stack panel or a grid.
GroupBox Overview
<GroupBox Header="Aðgerðir" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="270,0,0,178" Height="106" Width="176">
<StackPanel>
<CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
</StackPanel>
</GroupBox>

WPF Button with multiple text elements for 10 foot gui

I am trying to create buttons for a 10-foot GUI using WPF. Each button requires a little more data than just a single text string and an image, maybe 2-3 strings located in different positions and some imagery.
I have tried
<Button Height="52" HorizontalAlignment="Stretch" Name="button1" Width="407">
<Button.Content>
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
<TextBlock Name="textBloczk2" Text="ABC" TextAlignment="Left" DockPanel.Dock="Left"/>
<TextBlock Name="textBlxock1" Text="CDE" TextAlignment="Right" DockPanel.Dock="Right"/>
</DockPanel>
</Button.Content>
</Button>
But no matter which inner container I use, the button seems to disregard the layout from the DockPanel and the combined text ends up in the middle of the button. Am I doing something wrong or should I be using a different outer container ?
The problem seems to be that the DockPanel's width is so small that the Right and Left panels are the same width as your TextBlocks.
This seems to work as expected (setting the width of the DockPanel):
<Button Height="52" HorizontalAlignment="Stretch" Name="button1" Width="407">
<Button.Content>
<DockPanel LastChildFill="True" Width="300">
<TextBlock Name="textBloczk1" Text="Left" DockPanel.Dock="Left" />
<TextBlock Name="textBlxock2" Text="Right" DockPanel.Dock="Right" />
<TextBlock Name="textBlxock3" Text="Top" DockPanel.Dock="Top" />
<TextBlock Name="textBlxock4" Text="Bottom" DockPanel.Dock="Bottom" />
</DockPanel>
</Button.Content>
</Button>
Try to add "HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" to your button. This way dockpanel will occupy all space inside the button.

Accessing other listbox item controls and properties on button click

I'm currently binding an xml file to a listbox in C# WPF. Within the itemtemplate I added controls. as follows:
<DataTemplate x:Key="SinglecueTemplate">
<Grid Height="30" Width="425" Margin="3,3,0,3">
<Button Content="{Binding XPath=nr}" Width="30" Style="{DynamicResource CUEStyle_Button_Inhoudknopje}" Template="{DynamicResource CUEStyle_singlecueknopnummer}" Height="Auto" HorizontalAlignment="Left" Background="#FFABCCED" Foreground="White" IsEnabled="False"/>
<TextBlock x:Name="name" Margin="54,0,114.667,0" Width="Auto" VerticalAlignment="Center" FontSize="16" Foreground="Gray" Text="{Binding XPath=Name}"/>
<Button x:Name="playbutton" Width="30" Style="{DynamicResource CUEStyle_Button_Groot}" Template="{DynamicResource CUEStyle_Rondknopje}" Height="Auto" HorizontalAlignment="Right" Margin="0,0.55,74.737,-0.55" Content="u" FontFamily="Wingdings 3" Foreground="#FF0178D3" Opacity="1" BorderBrush="#FF0178D3" Click="playcue"/>
<Button Width="30" Style="{DynamicResource CUEStyle_Button_Groot}" Template="{DynamicResource CUEStyle_Rondknopje}" Height="Auto" HorizontalAlignment="Right" Margin="0,0.55,37.071,-0.55" Content="¢" FontFamily="Wingdings 2" Foreground="Gray" Opacity="0.4"/>
<Button Width="15" Style="{DynamicResource CUEStyle_Button_Groot}" Template="{DynamicResource CUEStyle_kleinloopje}" Height="15" HorizontalAlignment="Left" Margin="15,0,0,-5.5" Content="Q" FontFamily="Wingdings 3" Foreground="White" FontWeight="Bold" FontSize="10.667" Opacity="1" VerticalAlignment="Bottom" BorderBrush="{x:Null}" Background="{x:Null}" IsEnabled="{Binding XPath=Loop}"/>
<TextBlock Margin="0,0,114.667,0" Width="81.07" VerticalAlignment="Center" FontSize="16" Foreground="Gray" Text="03:02:11" HorizontalAlignment="Right"/>
</Grid>
</DataTemplate>
Now when a the playbutton is clicked I want to acces other properties from that listboxitem, for example the text from the text block. I looked up on how to do this and came up with the following:
private void playcue(object sender, System.Windows.RoutedEventArgs e)
{
Button playcue = (Button)sender;
Textbox titel = (Textbox)playcue.DataContext;
MessageBox.Show(titel);
}
But the code above gives me an error stating that the type Textbox is unknown. Do I have to do something with the datatemplate so that I can acces other items within the template? Or is it possible to access sibling nodes from the datasource?
UPDATE
Answer found. Now for future reference:
private void playcue(object sender, System.Windows.RoutedEventArgs e)
{
Button playcue = (Button)sender;
XmlElement name = (XmlElement)playcue.DataContext;
MessageBox.Show(name.InnerText);
}
Returns all sibling values from the item
If you want to access a specific sibling you can do so:
MessageBox.Show(name.SelectSingleNode("Name").InnerXml);
First problem is that it's TextBox not Textbox (C# is case sensitive.)
Second, it's unlikely that your DataContext is a TextBox. It should be an item in your ItemsSource collection. So, if the user clicks the third button in the list, you should be looking at the 3rd item in your ItemsSource collection. Since it appears you are using XML as a data source, I would guess that the item should be an XElement or an XmlElement.
I suggest you change Textbox titel = (Textbox)playcue.DataContext; to the following:
XElement element = (XElement)playcue.DataContext;
or
XmlElement element = (XmlElement)playcue.DataContext;
You will also need to change your MessageBox statement as well.

Categories