I have 4 radiobuttons and I want to check if any are checked.
This is my WPF code:
<StackPanel Background="#FF3A3A49" Grid.Column="1" Grid.Row="4">
<RadioButton x:Name="rtnRight" GroupName="answer" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="White" Content="value0" BorderBrush="White"/>
<RadioButton Content="value1" GroupName="answer" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
<RadioButton Content="value2" GroupName="answer" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="White" />
<RadioButton Content="value3" GroupName="answer" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="White" />
</StackPanel>
<Button x:Name="btnNext" Grid.Column="1" Grid.Row="5" Content="Dalej" VerticalAlignment="Center" HorizontalAlignment="Center" Width="100" Height="50" Margin="0 0 0 0 " Foreground="#FFAC0303" BorderBrush="#FFC1C1C1" Background="#66FFFFFF" Click="btnNext_Click"></Button>
After I click btnNext and no radiobuttons have been checked, I want to show a message dialog. How can I code this? This is my btnNext_Click function so far.
private async void btnNext_Click(object sender, RoutedEventArgs e)
{
if ("any radiobutton checked?")
{
await new Windows.UI.Popups.MessageDialog("Choose at least one answer").ShowAsync();
}
}
You can specify a name for your StackPanel and then check like:
if (!(radioButtonStackPanel.Children.OfType<RadioButton>().Any(rb => rb.IsChecked == true)))
Just remember to specify a name for StackPanel like:
<StackPanel Background="#FF3A3A49" Grid.Column="1" Grid.Row="4" x:Name="radioButtonStackPanel">
Related
On my Resources.xaml(Resource Dictionary) I have a DataTemplate :
<DataTemplate x:Key="ProductDetailsContentTemplate">
<StackPanel Orientation="Vertical" >
<Viewbox Margin="27,0,28,0" Height="200" >
<Image UWPUtil:ImageExtensions.CachedUriSource="{Binding ImageUri}" />
</Viewbox>
<TextBlock Text="Description" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding ProductDescription}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Barcode" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding Barcode}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Weight" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding Weight}" Style="{StaticResource SubtitleTextBlockStyle}" FontSize="30"/>
<TextBlock Text="Quantity" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" FontSize="24"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25*"/>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="25*"/>
</Grid.ColumnDefinitions>
<Button x:Name="SubtractQytyButton" Grid.Column="0" Content="-" FontSize="24" HorizontalAlignment="Stretch"
Margin="5,0" Style="{StaticResource TestButtonStyle}" FontWeight="ExtraBold"
Click="SubQytyButton_Click"/>
<TextBox x:Name="QuantityTextBox" Grid.Column="1" HorizontalAlignment="Stretch" FontSize="24" Margin="5,0"
KeyDown="Quantity_Keydown" TextChanging="Quantity_TextChanging"
Text="{Binding Quantity, Mode=TwoWay, Converter={StaticResource DecmialConverter}}" MaxLength="5" TextAlignment="Center"/>
<Button x:Name="AddQytyButton" Grid.Column="2" Content="+" FontSize="24" HorizontalAlignment="Stretch"
Margin="5,0" Style="{StaticResource TestButtonStyle}" FontWeight="ExtraBold"
Click="AddQytyButton_Click"/>
</Grid>
</StackPanel>
</DataTemplate><br>
I file nested it using the File Nesting from Mads Kristensen , here is the thread: https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting
so I have a Resources.xaml.cs on my ResourceDictionary
my problem is I want to set my QuantityTextBox Value on AddQytyButton and SubQytyButton click command, here is my click command event:
private void AddQytyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//does not exist in the current context
QuantityTextBox.Text = "test";
}
OR
private void AddQytyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Button btn = sender as Button;
// this is not working as null reference
var textbox = (btn.Parent as StackPanel).Children[1] as TextBox;
}
Thanks,
NicoTing
You should cast btn.Parent to a Grid instead of a StackPanel since you are using the former in your XAML.
var textbox = (btn.Parent as Grid).Children[1] as TextBox;
I have created a text box which when focused brings up a popup box with other text box's as options.. and when pressed closes the popup box.. but I'd like that to then take the value from whichever text box I have selected and add into the original text box. Is this possible? I'm new to wpf so forgive me!
Here is my code so far.
<Grid>
<StackPanel Margin="0,51,0,-51">
<TextBox x:Name="text" GotKeyboardFocus="text_GotKeyboardFocus" Margin="158,0,169,0" Height="24" Text="Select Your Time..." />
<Popup x:Name="popup" Width="282" Height="300" PlacementTarget="{Binding ElementName=text}">
<Grid>
<StackPanel>
<TextBox x:Name="text2" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="1 second" />
<TextBox x:Name="text3" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="2 second" />
<TextBox x:Name="text4" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="3 second" />
<TextBox x:Name="text5" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="4 second" />
<TextBox x:Name="text6" Background="White" Margin="10" GotKeyboardFocus="text_GotKeyboardFocus2" Cursor="Arrow" Height="24" Text="5 second" />
</StackPanel>
</Grid>
</Popup>
</StackPanel>
</Grid>
private void text_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
popup.IsOpen = true;
}
private void text_GotKeyboardFocus2(object sender, RoutedEventArgs e)
{
popup.IsOpen = false;
}
Get value from one textbox to another in xaml:
<TextBox Name="txtBox1"/>
<TextBox Name="txtBox2" Text="{Binding ElementName=txtBox1, Path=Text,UpdateSourceTrigger=PropertyChanged}" />
I hope I got your point.
Just add text.Text = (sender as TextBox).Text; to your second event handler as follows :
private void text_GotKeyboardFocus2(object sender, RoutedEventArgs e)
{
popup.IsOpen = false;
text.Text = (sender as TextBox).Text;
}
Good luck.
Get value from one textbox to another XAML
<TextBox Name="txtBox1"/>
<TextBox Name="txtBox2" Text="{Binding ElementName=txtBox1, Path=Text,UpdateSourceTrigger=PropertyChanged}" />
So I have my AML keypad as such :
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox Name="btnTotal" Width="280" Height="60" Grid.ColumnSpan="3" Grid.Row="0" Margin="10,10,10,10" Background="#302F37" Foreground="AntiqueWhite" FontSize="35"></TextBox>
<Button x:Name="btnZzero" Content="0" Width="80" Height="60" Grid.Column="0" Grid.Row="4" Margin="5,5,5,5" Background="#302F37" Foreground="White" Focusable="False" Click="btnZzero_Click"></Button>
<Button x:Name="btnOk" Content="OK" Width="80" Height="60" Grid.Column="1" Grid.Row="4" Margin="5,5,5,5" Click="btnOk_Click" Background="#FF8FC377" Focusable="False"></Button>
<Button x:Name="btnCancel" Content="Cancel" Width="80" Height="60" Grid.Column="2" Grid.Row="4" Margin="5,5,5,5" Click="cancel_Click" BorderBrush="Black" Background="#FFD64D4D" Focusable="False"></Button>
<Button x:Name="btnOne" Content="1" Width="80" Height="60" Grid.Column="0" Grid.Row="3" Margin="14,6,0,6" Focusable="False" Background="#302F37" Foreground="White" HorizontalAlignment="Left" Click="btnOne_Click"></Button>
<Button x:Name="btnTwo" Content="2" Width="80" Height="60" Grid.Column="1" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnTwo_Click"/>
<Button x:Name="btnThree" Content="3" Width="80" Height="60" Grid.Column="2" Grid.Row="3" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnThree_Click"/>
<Button x:Name="btnFour" Content="4" Width="80" Height="60" Grid.Column="0" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnFour_Click"></Button>
<Button x:Name="btnFive" Content="5" Width="80" Height="60" Grid.Column="1" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnFive_Click"></Button>
<Button x:Name="btnSix" Content="6" Width="80" Height="60" Grid.Column="2" Grid.Row="2" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnSix_Click"></Button>
<Button x:Name="btnSeven" Content="7" Width="80" Height="60" Grid.Column="0" Grid.Row="1" Margin="12,5,9,6" Focusable="False" Background="#302F37" Foreground="White" Click="btnSeven_Click"></Button>
<Button x:Name="btnEight" Content="8" Width="80" Height="60" Grid.Column="1" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnEight_Click"></Button>
<Button x:Name="btnNine" Content="9" Width="80" Height="60" Grid.Column="2" Grid.Row="1" Margin="5,5,5,5" Focusable="False" Background="#302F37" Foreground="White" Click="btnNine_Click"></Button>
Now I want to write a small helper method to run when each button is clicked but I cannot work out how to use the Content from the XAML.
for example this works for button 1 :
private void btnOne_Click(object sender, RoutedEventArgs e)
{
addButtonNumberToResult();
}
And the helper method uses btnOne.Content but how do I get it to use the Content from any button being pressed? So I can just add the helper methosto each button?
private void addButtonNumberToResult() //helper method
{
if (btnClicked == true)
{
btnTotal.Text = "";
btnClicked = false;
}
QuantityResult = QuantityResult += btnOne.Content;
btnTotal.Text = QuantityResult;
}
You can use the sender variable to determine the button clicked:
private void btn_Click(object sender, RoutedEventArgs e)
{
addButtonNumberToResult((Button)sender);
}
add btn_Click to the Click event of each button (and replace the ones you have already)
private void addButtonNumberToResult(Button btn) //helper method
{
if (btnClicked == true)
{
btnTotal.Text = "";
btnClicked = false;
}
QuantityResult = QuantityResult += btn.Content;
btnTotal.Text = QuantityResult;
}
In a group of toggle switches I couldn't control the any toggle switch individually, rather if I try to change one then all the values change simultaneously.
My Xaml:-
<ListBox HorizontalAlignment="Left" ScrollViewer.VerticalScrollBarVisibility="Disabled" Margin="0,0,0,0" Name="yourChoiceListBox" VerticalAlignment="Top" Width="476" ItemsSource="{Binding yourChoiceList, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray" BorderThickness="1">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" Width="476" Height="60">
<TextBlock Margin="10,0,0,0" FontSize="24" FontWeight="SemiBold" Foreground="Black" Width="250" Text="{Binding title}" VerticalAlignment="Center" Height="35" TextTrimming="WordEllipsis" />
<TextBlock FontSize="24" FontWeight="SemiBold" Foreground="Black" Width="150" Text="{Binding valueDesc}" HorizontalAlignment="Right" FlowDirection="RightToLeft" VerticalAlignment="Center" Visibility="{Binding valueVisible}" />
<toolkit:ToggleSwitch x:Name="toggle" Foreground="Black" Content="{Binding toggleContent}" IsChecked="{Binding DataContext.isCheck,ElementName=yourChoiceListBox,Mode=TwoWay}" Height="110" Width="198" HorizontalAlignment="Right" Margin="40,-15,0,0" Visibility="{Binding tongleVisible}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My ViewModel CS:-
public static string _toggleContent;
public string toggleContent
{
get { return _toggleContent; }
set { this.RaiseAndSetIfChanged(x => x.toggleContent, value); }
}
public static bool _isCheck;
public bool isCheck
{
get { return _isCheck; }
set
{
this.RaiseAndSetIfChanged(x => x.isCheck, value);
if (isCheck)
toggleContent = "Yes";
else
toggleContent = "No";
}
}
Here i want to change the toggle switch content in to "Yes" or "No". If i choose 'On' the content should be "Yes". But here if i select one toggle switch all the toggle switch is changed. Please let me any idea to solve my problem. Thanks in advance.
This is your ToggleSwitch:
<toolkit:ToggleSwitch x:Name="toggle" Foreground="Black" Content="{Binding toggleContent}"
IsChecked="{Binding DataContext.isCheck,ElementName=yourChoiceListBox,Mode=TwoWay}"
Height="110" Width="198" HorizontalAlignment="Right" Margin="40,-15,0,0"
Visibility="{Binding tongleVisible}" />
the problem is here:
IsChecked="{Binding DataContext.isCheck,ElementName=yourChoiceListBox,Mode=TwoWay}"
You are binding the IsChecked property of the ToggleSwitch to the isCheck property of your list.
You should bind the IsChecked property of the ToggleSwitch to the isCheck property of your list item like this:
IsChecked="{Binding isCheck, Mode=TwoWay}"
In my wpf application, there is View class where I've ListBox. I wrote the code for double click event of ListBox Item.so when I double click on any list Box Item that item will be posted in my Harvest account.Here is the event:
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
try
{
ListBoxItem item = (ListBoxItem)sender;
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)item.DataContext;
if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project" && entryToPost.ClientNameBinding == "Select Client")
MessageBox.Show("Please select you Project and Client");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
MessageBox.Show("Entry posted");
}
else
{
//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}
}
catch (Exception)
{ }
}
My xaml code:
<DataTemplate x:Key="DefaultDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<TextBox Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
<TextBox Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
</StackPanel>
</DataTemplate>
<!-- Editable DataTemplate -->
<DataTemplate x:Key="EditableDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<ComboBox x:Name="ClientComboBox" SelectionChanged="ProjectComboBoxChanged" ItemsSource="{Binding Path=clientList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ClientNameBindingClass, Mode=OneWayToSource}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<TextBox Text="{Binding StartTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
<TextBox Text="{Binding StopTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
<ComboBox x:Name="ProjectComboBox" SelectionChanged="ProjectComboBoxChanged" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" Width="71" Background="Yellow" BorderThickness="0"/>
</StackPanel>
</DataTemplate>
<!-- DataTemplate Selector -->
<l:DayViewListDataTemplateSelector x:Key="templateSelector"
DefaultDataTemplate="{StaticResource DefaultDataTemplate}"
EditableDataTemplate="{StaticResource EditableDataTemplate}"/>
I've timer in my class which generates that EditableDataTemplate with two comboBoxes. My problem is, when I select Client and Project in ComboBoxes and double click on the entry, it's posted in my account but at that time I want it to convert from editableDataTemplate to DefaultDataTemplate (i.e those two comboboxes should become textboxes likewise in DefaultDataTemplate). How should I achieve this result?
I don't think the DataTemplateSelector offers a method of changing the data template on request, it is merely used to choose different templates for different types of data (not data states).
I think probably the best way would be to add a property, lets call it IsInEditMode, to your data model. You could then add both the TextBlock and the Combobox to your data template and toggle their visibility according to the value of IsInEditMode.
By the way: if you use the ListBox.SelectedItem property in your DoubleClick-eventhandler you can directly access the data model element without having to first get the ListBoxItem and then access
its data context.
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
try
{
if(listBox1.SelectedItem is Harvest_TimeSheetEntry)
{
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)listBox1.SelectedItem;
if (!entryToPost.isSynced)
{
//Check if something is selected in selectedProjectItem For that item
if (entryToPost.ProjectNameBinding == "Select Project" && entryToPost.ClientNameBinding == "Select Client")
MessageBox.Show("Please select you Project and Client");
else
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
MessageBox.Show("Entry posted");
entryToPost.IsInEditMode = true; //set edit mode!
}
}
else
{
//Already synced.. Make a noise or something
MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}
}
catch (Exception)
{ }
}