C# WPF XAML , How create Textbox input field with stringformat? - c#

Code XAML
<Grid>
<StackPanel Orientation="Horizontal">
<Label Content="Bank Doc Nbr :" Margin="0,0,0,0" VerticalAlignment="Center" />
<TextBox Name="txtBankNbr" Text="{Binding ElementName=txtBankNbr, Path=Text, StringFormat={}{0:##0/000}}" materialDesign:HintAssist.Hint="0/000" VerticalAlignment="Center" materialDesign:TextFieldAssist.DecorationVisibility="Hidden" BorderThickness="0" />
<Label Content="Bank Income :€" Margin="5,0,0,0" VerticalAlignment="Center" />
<TextBox Name="txtBankIncome" Text="{Binding ElementName=txtBankIncome, Path=Text,StringFormat={}{0:######0.00}}" materialDesign:HintAssist.Hint="0,00" VerticalAlignment="Center" materialDesign:TextFieldAssist.DecorationVisibility="Hidden" BorderThickness="0" />
</StackPanel>
</Grid>
------ Explanation: Format of txtBankNbr is {0:##0/000} and can only receive numbers. eg "1/001" , 12/021 , ....
Format of txtBankNbr is {0:##0.00} and can only receive a decimal
number What is wrong in my code?
Thanks

Try this:
StringFormat={}{0:#.##}
it should show two decimal place

Related

WPF: Can I show one double in two different ways? [duplicate]

This question already has answers here:
Rounding a Value in the WPF Binding
(3 answers)
Closed 2 years ago.
I am creating a "Progress-Bar-Clock", where the Time gets shown as percentages. For example, this is the time right now:
Xaml:
<Viewbox>
<Grid>
<Grid.DataContext>
<vm:TimerVM/>
</Grid.DataContext>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,10,10,10">
<Border BorderBrush="Gray" BorderThickness="1">
<Label Content="Progress Bar Timer" FontFamily="Bold" FontSize="45" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,0,20"/>
</Border>
<StackPanel>
<Label Content="Seconds in this Minute:"/>
<Grid Width="400" Height="25">
<ProgressBar Value="{Binding SecondsInMinute}" />
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
<Run Text="{Binding SecondsInMinute}"/><Run Text="%"/>
</TextBlock>
</Grid>
</StackPanel>
<StackPanel>
<Label Content="Minutes in this hour:"/>
<Grid Width="400" Height="25">
<ProgressBar Value="{Binding MinutesInHour}" />
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
<Run Text="{Binding MinutesInHour}"/><Run Text="%"/>
</TextBlock>
</Grid>
</StackPanel>
<StackPanel>
<Label Content="Hours in this day:"/>
<Grid Width="400" Height="25">
<ProgressBar Value="{Binding HoursInDay}" />
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
<Run Text="{Binding HoursInDay}"/><Run Text="%"/>
</TextBlock>
</Grid>
</StackPanel>
<StackPanel Margin="0,50,0,0">
<Label Content="Time until timer finished:"/>
<Grid Width="400" Height="25">
<ProgressBar Value="{Binding TimerInPercent}" />
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
<Run Text="{Binding TimerInPercent}"/><Run Text="%"/>
</TextBlock>
</Grid>
</StackPanel>
</StackPanel>
</Grid>
</Viewbox>
The thing that disturbs me is the percentage. At the moment I show it with only two digits, so it's easier to read, but that also affects the progress bar.
This is the method, my second thread executes to update the timer:
private void UpdateRelative()
{
while (true)
{
DateTime time = DateTime.Now;
SecondsInMinute = Math.Round(time.Second / 0.6);
MinutesInHour = Math.Round(time.Minute / 0.6);
HoursInDay = Math.Round(time.Hour / 0.24);
Thread.Sleep(timeout);
}
}
I would like to use a non-rounded up number for the progress bar, but a rounded one for the text on the progress bar. Is that possible without creating a second property that the label would use?
You could format the displayed value in the view instead of rounding the actual value:
<Run Text="{Binding MinutesInHour, StringFormat=N0}"/><Run Text="%"/>
Code:
MinutesInHour = time.Minute / 0.6;

XAML textbox updated when Textis entered in input

I have a bunch of inputs that are being used to collect information. These inputs need to update the listname textbox on update.
I need to display it as Last Name space Suffix, First Name space Middle Initial. the Parth on each input is setting the text thats typed into the code to save it to the database on update. I am not sure where to go from here. Do I use a one way or two way mode? do I write it in c# and how?
<TextBox x:Uid="TextBox_1" Grid.Column="1" Grid.Row="0" Tag="{Binding Path=FirstNameLabel, Source={StaticResource Clientization}}" Style="{StaticResource EditTextBox}" MaxLength="35"
Text="{Binding Path=Provider.FirstName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_2" Grid.Column="2" Grid.Row="0" Tag="Middle" MinWidth="75" Style="{StaticResource EditTextBox}" MaxLength="30"
Text="{Binding Path=Provider.MiddleName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_3" Grid.Column="1" Grid.Row="1" Tag="{Binding Path=LastNameLabel, Source={StaticResource Clientization}}" Style="{StaticResource EditTextBox}" MaxLength="60"
Text="{Binding Path=Provider.LastName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_4" Grid.Column="2" Grid.Row="1" Tag="Suffix" Style="{StaticResource EditTextBox}" MaxLength="20"
Text="{Binding Path=Provider.Suffix, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_5" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Tag="List Name*" Style="{StaticResource EditTextBox}" MaxLength="160"
Text="{Binding Path=Provider.ListName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_6" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="3" Tag="NPI*" Style="{StaticResource EditTextBox}" MaxLength="80"
Visibility="{Binding Path=HideNpi, Source={StaticResource Clientization}, Converter={StaticResource TernaryConverter}, ConverterParameter='True ? Collapsed : Visible'}"
Text="{Binding Path=Provider.NPI, UpdateSourceTrigger=PropertyChanged}"/>
I suggest you to use Entity Framework with your WPF application to binding with database, please check out below links for your reference:
http://msdn.microsoft.com/en-us/vstudio/Video/dd776537
http://msdn.microsoft.com/en-us/vstudio/Video/dd776540
And this:http://msdn.microsoft.com/en-us/library/cc716735.aspx
Hope it helps.

SL4 Dataform NewItemTemplate textbox value not getting bound

I have a silverlight 4 application using MVVM which has a grid and dataform. the dataform
has an EditItemTemplate and a NewItemTemplate. On Editing the EditItemTemplate code runs fine but upon adding a new item the NewItemTemplate code throws an error to the effect that barcode is required. Where could I be going wrong. Thanks in advance.
NewItemTemplate xaml code
<toolkit:DataField Label="Barcode :"
FontWeight="Bold"
Grid.Column="0"
Grid.Row="0"
Margin="1"
IsRequired="True"
HorizontalAlignment="Left">
<TextBox Grid.Column="1"
Name="BarcodeId"
Text="{Binding Barcode.BarcodeId,Mode=TwoWay, NotifyOnValidationError=True,ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Stretch"
Grid.Row="0"
Margin="2"
Width="300"
TabIndex="0" />
</toolkit:DataField>
EditItemTemplate code
<toolkit:DataField Label="Barcode :"
FontWeight="Bold"
Grid.Column="0"
Grid.Row="0"
Margin="1"
IsRequired="False"
IsEnabled="True"
HorizontalAlignment="Left">
<TextBox Width="200"
Text="{Binding Barcode.BarcodeId,Mode=TwoWay}" FontWeight="Normal" />
</toolkit:DataField>

How to Validate 4 Sequential Button Clicks (WPF Application)

What I am having trouble figuring out:
- How to capture 4 button clicks and check if they match the correct sequence similar to entering a 4 digit ATM debit card PIN number
I have a WPF Application and added a new Window that has a PIN pad, numbers 0-9, a question mark button (used as a help button), a backspace button (in case you clicked the wrong number), a TextBox at the top which displays a dot instead of the number (Wingdings - letter 'l'), and a hidden TextBlock which would show an Incorrect PIN message if the numbers were not entered in the correct order. For the time being, I am just going to hard code the 4 digit PIN (7410 for example) since my main goal is learning how to capture and validate the button click sequence. Since I will not be using an Enter key button, I want to continue to the next page as soon as the last number is clicked and the sequence was correct, else display the notification message that the PIN is incorrect. I will also put code in to not allow anything except mouse clicks work and limit the TextBox to only allow a max of 4 digits.
I'm guessing I need a method that is called on each button click which keeps track of the sequence and loops back through each time until all 4 numbers are clicked in the correct sequence then moves on to the next page. Sorry if I'm not explaining well, still learning, so if you need more details I'll do my best to give more.
Thank you in advance.
Below is the xaml code for the PIN pad.
<Border BorderThickness="1" BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border Background="Black" Height="50" Grid.ColumnSpan="3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<TextBlock x:Name="PINTextBlock" Foreground="White" FontSize="18" FontFamily="Wingdings" VerticalAlignment="Center" HorizontalAlignment="Center" MaxWidth="4" />
</Border>
<TextBlock x:Name="ErrorMessageTextBlock" Foreground="Red" Visibility="Collapsed" Grid.ColumnSpan="3" Grid.Row="1" />
<StackPanel Orientation="Horizontal" Grid.Row="2">
<Button x:Name="SevenButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="SevenButton_Click" TabIndex="9">
<TextBlock Text="7" FontSize="24" FontWeight="Bold" />
</Button>
<Button x:Name="EightButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="EightButton_Click" TabIndex="10">
<TextBlock Text="8" FontSize="24" FontWeight="Bold" />
</Button>
<Button x:Name="NineButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="NineButton_Click" TabIndex="11">
<TextBlock Text="9" FontSize="24" FontWeight="Bold" />
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="3">
<Button x:Name="FourButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="FourButton_Click" TabIndex="6">
<TextBlock Text="4" FontSize="24" FontWeight="Bold" />
</Button>
<Button x:Name="FiveButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="FiveButton_Click" TabIndex="7">
<TextBlock Text="5" FontSize="24" FontWeight="Bold" />
</Button>
<Button x:Name="SixButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="SixButton_Click" TabIndex="8">
<TextBlock Text="6" FontSize="24" FontWeight="Bold" />
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="4">
<Button x:Name="OneButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="OneButton_Click" TabIndex="3">
<TextBlock Text="1" FontSize="24" FontWeight="Bold" />
</Button>
<Button x:Name="TwoButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="TwoButton_Click" TabIndex="4">
<TextBlock Text="2" FontSize="24" FontWeight="Bold" />
</Button>
<Button x:Name="ThreeButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="ThreeButton_Click" TabIndex="5">
<TextBlock Text="3" FontSize="24" FontWeight="Bold" />
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="5">
<Button x:Name="QuestionMarkButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="QuestionMarkButton_Click" TabIndex="0">
<TextBlock Text="?" FontSize="24" FontWeight="Bold" />
</Button>
<Button x:Name="ZeroButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="ZeroButton_Click" TabIndex="1">
<TextBlock Text="0" FontSize="24" FontWeight="Bold" />
</Button>
<Button x:Name="BackspaceButton" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" Height="50" Click="BackspaceButton_Click" TabIndex="2">
<TextBlock Text="Õ" FontFamily="Wingdings" FontSize="24" FontWeight="Bold" />
</Button>
</StackPanel>
</Grid>
</Border>
You can do the validation using Xaml and converters. All you have to do is;
Use binding with ErrorTextBlock and listen for PinTextBlock.Text and run it through converter which will do the actual validation.
This way the validation logic will be seperated nicely and will be reusable. There are other WPF inbuilt validation ways and you can google about them. (IDataErrorInfo and ValidationRules). This would be the "WPF" way.
But the learning curve is little steep. If you just wanna finish this thing, then yes, add this to StackPanel: Button.Click="buttonClickHandler", and in code-behind you will be recieve every button you click in single place. And do your calculations there.
I would have a string that gets appended to (or modified in the case of Backspace or Clear) with each button click, and base my validation off if that string is 4 characters long and equals the PIN #
If it's 4 characters and matches, move to next page. If it's 4 characters and doesn't match, display the error message.
I know this is a learning application, but I'd highly recommend learning the MVVM design pattern if you will be working with WPF.
In that case, I'd have something like a List<KeyValuePair<string, ICommand>> Buttons that gets bound to an ItemsControl with an ItemsPanelTemplate set to a UniformGrid with 3 rows and 3 columns, and the ItemTemplate set to a Button that binds the Content to the Key and the Command to the Value, and an bool IsErrorDisplayed value that gets set to true anytime the error message should be displayed.
<TextBlock x:Name="ErrorMessageTextBlock" Foreground="Red"
Visibility="{Binding IsErrorDisplayed, Converter={StaticResource BooleanToVisibiltyConverter}}"
Grid.ColumnSpan="3" Grid.Row="1" />
<ItemsControl ItemsSource="{Binding Buttons}">
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" Rows="3" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- ItemTemplate -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Width="50" Height="50" Command="{Binding Value}">
<TextBlock Text="{Binding Key}" FontSize="24" FontWeight="Bold" />
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I'd also probably do something to secure the PIN # so isn't being stored as plain text.
Associate them all to the same event handler. In that event uselogic to associate the x:Name with an Integer and build up the 4 digit PIN. This just lets you consolidate logic.
private void one_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
Int32 num;
switch (btn.Name)
{
case "one":
num = 1;
break;
case "two":
num = 2;
break;
default:
// problem
}
if (PIN > 1000) // logic
// now you have you have num and can deal with the logic in one click event;
PIN = (PIN * 10) + num;
if (PIN = correctPIN)
{
}
else
{
}
}

Data binding in pivot TitleTemplate for Windows Phone

I must be missing something simple here...I am writing a Windows Phone 7 app and I have customized my pivot header to be the following:
<controls:Pivot Name="InfoPivot">
<controls:Pivot.TitleTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0" VerticalAlignment="Top">
<Rectangle Fill="{Binding CategoryFill}" Height="50" Width="50" Margin="355,25,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Name="CategoryRect" />
<StackPanel Margin="-425,-14,0,0" Width="432">
<TextBlock x:Name="StationTitle" Text="{Binding StationTitle}" Margin="10,0,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
<TextBlock Name="LocationTitle" Text="{Binding LocationTitle}" TextWrapping="Wrap" Margin="12,0,0,20" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</controls:Pivot.TitleTemplate>
When I navigate to this page, I pass Station and Location as parameters, and in the OnNavigatedTo() for this page I try to set the StationTitle and LocationTitle. Unfortunately, I end up getting:
Error 2 The name 'StationTitle' does not exist in the current context
How should/do we go about accessing members in the Pivot TitleTemplate? Any help will be appreciated! Thanks.
Stop using the title template and binding to acheive this. Try this instead:-
<controls:Pivot Name="InfoPivot">
<controls:Pivot.Title>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0" VerticalAlignment="Top">
<Rectangle Fill="{Binding CategoryFill}" Height="50" Width="50" Margin="355,25,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Name="CategoryRect" />
<StackPanel Margin="-425,-14,0,0" Width="432">
<TextBlock x:Name="StationTitle" Margin="10,0,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
<TextBlock x:Name="LocationTitle" TextWrapping="Wrap" Margin="12,0,0,20" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</StackPanel>
</controls:Pivot.Title>
Then in code-behind:-
if (NavigationContext.QueryString.TryGetValue("Station", out station))
{
StationTitle.Text = station;
}
if (NavigationContext.QueryString.TryGetValue("LocationTitle", out locationTitle))
{
LocationTitle.Text = locationTitle;
}
Without seeing all of your code, this may be as simple as making the Name declaration consistent on your two TextBlocks.
Note, one is Name="", the other is x:Name="".
Defining the template inline worked for me, The TitleTemplate just wouldn't work.
<phone:Pivot>
<phone:Pivot.Title>
<TextBlock Margin="0"
Text="{Binding Exercise.Name}"
Style="{StaticResource MainTitleStyle}"/>
</phone:Pivot.Title>
</phone:Pivot>

Categories