WPF C# WrapPanel's content to be placed nicely - c#

From what i know about WrapPanel , once it reaches its limit , it will auto move to the next line but however is there anyway to make this nicer without adding extra panel ?
Now its something like this ( an example ) :
How you like to live on a houseboat with your family and pets [
textbox ] ( lots
/ all ) year round?
As you can see the brackets ( lots / all ) is being separated , is there any way to make them go to next line if they are together without adding extra panel ?
My xaml code :
<Grid Background="#FFF0FA08">
<WrapPanel x:Name="WrapPanelTest" HorizontalAlignment="Center" Height="658" VerticalAlignment="Center" Width="1366">
</WrapPanel> // this Panel i am talking about.
<StackPanel Height="110" Name="stackPanel1" Width="200" HorizontalAlignment="Left" VerticalAlignment="Bottom" >
<Button Content="Check!" Height="100" Name="button1" Width="125" FontSize="35" Click="button1_Click" />
</StackPanel>
</Grid>
How i add populate those content inside the wrap panel :
Basically i just retrieve it from database and create them dynamically and add to
wrappanel.children.add
, because the code is too long and i duno if it is related . I tried to add multiple panels inside to make it look nicer but i can only have one panel because thats the most easiest way i can find my controls. ( textboxes ) thats why i need to know if there is any way to do this without adding any panels

Forget the wrappanell and just use TextWrapping="Wrap" in a textblock you can use WrapWithOverflow to wrap on whole words
But (lots / all) will still be separated, so you will have to remove the spaces and make it (lots/all)

Related

displaying the square root of "X" in label

I have written a simple calculator program in C# which can also calculate the square root of any number.
So, I was just wondering if you could display said number in a label. Or rather display it like you would when calculating in real maths where the number is under the square root
(https://images-na.ssl-images-amazon.com/images/I/51Vo0t5w5YL.jpg).
Is that even a possibility? For those curious:
//Root
if (CW3.Checked) //Square Root Box checked
{
if (Z3N.Checked) //Negativ Number Box checked
{
MessageBox.Show("You can't get the square root of negative numbers.");
}
else
{
Zahl3 = Math.Sqrt(Zahl3);
}
}
It sounds like it's just a small project, so perhaps you could use something like this:
<Grid HorizontalAlignment="Center">
<Label Content="13.2" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Label Content="√" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="18" Margin="-11,-2,0,0"/>
<Grid Height="1" Background="Black" Margin="3,-13,3,0"/>
</Grid>
Just copy it all and insert it instead of the label you have. And change the number - bind the content or edit from code behind or whatever you do. There might be some fidgeting with the margins, but that is all part of the fun. TextBlocks might be the way to go instead of labels though.

Formatted Textboxes for UWP

I was wondering if anybody had any recommendations on how I could format a textbox for time inputs(hh:mm:ss) as well as Coordinates in Degrees,Minutes, Seconds (dd°mm'ss"). I am well aware that you can set the format of a string by the String.Format() method.... this does give me the desired layout in the textbox, but I want a control that has the appropriate symbols locked in and will autotab over those symbols as the user gives their inputs.
I have been searching for a way to do this in C#/UWP for a while with no luck, if anybody has any suggestions or could point me in the right direction it would be greatly appreciated.
The UWP Community Toolkit has some very good controls which are easy to integrate and one of them is the TextBox Mask control which is ideal for your scenario.
You can also try out the TextBoxRegex control which is not quiet what you want but is indeed something you should check out before proceeding.
Please reach out in case you are having difficulty in integrating the control for your specific requirement.
Optionally, you can download the sample app for the UWP toolkit to know about all the available controls /Tools. (UWP Community Toolkit Sample App)
EDIT 1:
As you might have noticed, the default behaviour for tab is that it sets the focus to the next control. Thus, having a single control will not be ideal since on press of tab, your textbox would lose focus and you would need to update the text and set the focus back to your textbox.
A relatively simpler way would be the following:
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1">
<StackPanel.BorderBrush>
<SolidColorBrush Color="{ThemeResource SystemBaseHighColor}"/>
</StackPanel.BorderBrush>
<TextBox x:Name="hour" MaxLength="2" BorderBrush="{x:Null}" VerticalAlignment="Center" HorizontalAlignment="Center" LostFocus="hour_LostFocus"></TextBox>
<TextBlock Text=":" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="minute" MaxLength="2" BorderBrush="{x:Null}" HorizontalAlignment="Center" VerticalAlignment="Center" LostFocus="minute_LostFocus"></TextBox>
<TextBlock Text=":" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBox x:Name="second" MaxLength="2" BorderBrush="{x:Null}" HorizontalAlignment="Center" VerticalAlignment="Center" LostFocus="second_LostFocus"></TextBox>
</StackPanel>
This is basically your custom build control/usercontrol. So that when user tabs out it moves to the next textbox inside the stackpanel and so on.
In your hour_LostFocus event you can validate the textbox's current value and append 0 and do other validations .
private void hour_LostFocus(object sender, RoutedEventArgs e)
{
string val = (sender as TextBox).Text;
Regex regex = new Regex(#"^([0[0-9]|1[0-9]|2[0-3])$");
Match match = regex.Match(val);
if (!match.Success)
{
//append 0 and other validations
}
}
The same applies for minute and seconds .. just that the regex would be different in these cases.

Display multiline DynamicResource Label

I wish to add a Label in WPF that displays string from two different DynamicResources.
I want each DynamicResource to be on a new line.
My existing code is:
<Label x:Name="MyTextDisplay"
Grid.Row="3"
Grid.ColumnSpan="2"
Background="Red"
BorderBrush="Blue"
BorderThickness="1"
Margin="2, 2, 2, 2">
<TextBlock TextWrapping="Wrap" Text="{DynamicResource MyTextLine1}" Grid.ColumnSpan="2"/>
</Label>
I have another DynamicResource called MyTextLine2 that I want to display below MyTextLine1 but in the same Label.
How can I do this?
I have looked at these examples here but they dont display on new lines: How to bind multiple values to a single WPF TextBlock?
I faced the same problem and finally I found a solution.
Just use \r\n linebreak instead of just \n.
So, your resourse must look like:
<system:String x:Key="MyText" xml:space="preserve">Line 1
Line 2</system:String>
I realy don't know why this notation must be used only for dynamic resources, but it works for me

remove a element from a StackPanel and add it to other StackPanel

is it possible to remove a item e.g. a TextBlock from a StackPanel and add it to a other StackPanel?
My XAML Code looks like this:
<StackPanel x:Name="PanelStack1" VerticalAlignment="Bottom" Margin="0,0,0,86">
<Rectangle x:Name="RecX" />
</StackPanel>
<StackPanel x:Name="PanelStack2" VerticalAlignment="Bottom" Margin="0,0,0,86" />
I want to move the RecX from the PanelStack1 to PanelStack2 via my C# Code. Is this possible?
This is just a small example, in my real code there are many elements which have to been moved.
Try this in your code behind:
PanelStack1.Children.Remove(RecX);
PanelStack2.Children.Add(RecX);

How to display a Button which hosts TextBlock with the style PhoneTextSubtleStyle?

I need to create a button with two lines of text:
The first one is Command Title like "Save"
The second one is a Description of the Command like "The application state will be saved"
So I have written the next xaml:
<Button Margin="0,128,0,0" Padding="10,5" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<StackPanel Margin="0" UseLayoutRounding="False">
<TextBlock FontSize="{StaticResource PhoneFontSizeMediumLarge}" FontFamily="{StaticResource PhoneFontFamilySemiBold}">Save</TextBlock>
<TextBlock Style="{StaticResource PhoneTextSubtleStyle}" Margin="0">The application state will be saved</TextBlock>
</StackPanel>
</Button>
This code working well except a one issue. The Description line becomes invisible when the button is pushed.
I'm sure the root cause is the low contrast color of the description line. But I don't know how to fix it.
Update: I have tried to use the PhoneTextSubtleStyle style but still have the same issue.
You could retemplate the Button (using the Control.Template property) to look different so that when pushed it no longer interferes with the content.
Could you try something like this
System.Windows.Visibility.Visible;
System.Windows.Visibility.Hidden;
or
System.Windows.Visibility.Collapsed
here is a link that will show an example of how to use this inside of a StackPanel
How to: Change the Visibility Property

Categories

Resources