reformat XAML in VS 2013 or with Resharper - xaml

I have several question about formatting XAML code with Resharper or VS 2013.
How can I set up in Resharper Code Cleanup or in VS add empty line
between XAML / XML tags?
Before:
<Canvas Background = "LightSkyBlue">
<TextBox x:Name = "TextBoxGreen"
Canvas.Left = "150"
Canvas.Top = "50"
MinWidth = "200"
BorderThickness = "3"
BorderBrush = "GreenYellow"
MouseDown = "TextBox_OnMouseDown">
Text from green textBox
</TextBox>
</Canvas>
After
<Canvas Background = "LightSkyBlue">
<TextBox x:Name = "TextBoxGreen"
Canvas.Left = "150"
Canvas.Top = "50"
MinWidth = "200"
BorderThickness = "3"
BorderBrush = "GreenYellow"
MouseDown = "TextBox_OnMouseDown">
Text from green textBox
</TextBox>
</Canvas>
I would like order XAML attributes in logical order or alphabetical
order. After my research I found these options:
Use Xaml Markup Styler Extension but not useful for me because I
use VS 2013. Port to VS 2013 is not finish yet.
Use Xaml Attribute Ordering Plugin to Resharper but this plugin
doesn’t order event handlers for example.
So it exist way how order XAML attributes in alphabetical order? Or how you order XAML attributes?

select your code, and press
Alt+Enter
and select
Format Selection

i think is too late for this answer but the solutions is quite simple. The shortcut keys are: CTRL + K + D in VS2013. that´s the middle of the question.
I hope it's useful.

Related

Border element with a background hides the other element under it. (UWP)

I have a grid with multiple buttons as children. Now I want to give this Grid rows a background color. So I added a border with a background color like this
Border brd = new Border
{
Margin = new Thickness(0, 2, 0, 2),
Background = (SolidColorBrush) Application.Current.Resources["RedBrush"],
CornerRadius = new CornerRadius(22),
};
Grid.SetColumn(brd,startColumn);
Grid.SetColumnSpan(brd, 9- startColumn);
Grid.SetRow(brd, startRow);
GridMain.Children.Add(brd);
All looks good to me, But when this is rendered all buttons are hidden as the Background color of Border only is visible in the row. How can I overcome this?
Instead of adding the Border from code behind, If I added in Xaml all works fine (border background is visible as button background and button text is visible)
<Border Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="8" Background="{StaticResource RedBrush}" CornerRadius="22" Margin="0 2 0 2" />
But for various reasons I wan the ability to add this border from code behind itself.
If you want to set the background color for Grid, you can directly use Grid.Background to set, without adding a new Border.
But if you need to add a Border as a background, please pay attention to the calling sequence in the code.
Generally speaking, the element added later will be placed on the top layer of the previous element, forming a visual effect of covering.
From your problem description. You can put the code that adds Border first.
Thanks.

Windows Phone ToolKit Map Error "Items must be empty before using Items Source"

I have a Windows XAML page which contents a map as below from the codebehind using my viewmodels constructor. I want to display multiple locations in Map with a pointer so I am using Windows Phone toolkit
<Controls:Map x:Name="AllyMap" Grid.Row="1" Center="{Binding GeoLocation}" ZoomLevel="12">
<toolkit:MapExtensions.Children>
<toolkit:MapItemsControl ItemsSource="{Binding MapList}">
<toolkit:MapItemsControl.ItemTemplate>
<DataTemplate>
<toolkit:Pushpin GeoCoordinate="{Binding GeoCoordinate}" Content="{Binding Content}"/>
</DataTemplate>
</toolkit:MapItemsControl.ItemTemplate>
</toolkit:MapItemsControl>
</toolkit:MapExtensions.Children>
</Controls:Map>
Now I am binding with MapList Itemsource as below.
atmLocationsMapView = new ATMLocationsMapViewModel();
this.DataContext = atmLocationsMapView;
ObservableCollection<DependencyObject> Mapchildren = MapExtensions.GetChildren(AllyMap);
MapItemsControl AllyMapObject;
AllyMapObject = null;
AllyMapObject = Mapchildren.FirstOrDefault(x => x.GetType() == typeof(MapItemsControl)) as MapItemsControl;
AllyMapObject.ItemsSource = atmLocationsMapView.MapList;
It is working fine with a first iteration. I have a feature to filter among the maps. When I am filtering it I am getting "Items must be empty before using Items Source" in the last line of the above code.
Can anybody please help me with this.

How to Assign a XAML Stype from Common/StandardStyles.xaml in code behind

I have search and tried a number of things to do this. I have a style that is successfully bound to multiple XAML frames by doing XAML code like this, the name of the style is ViewPersonTextboxDataStyle:
<TextBox Grid.Row="5" Grid.Column="1" Name="textboxName" Text="{Binding textboxName}" Margin="5,5,5,5" Style="{StaticResource ViewPersonTextboxDataStyle}"/>
So, when I get to another frame where I want to create the Grid rows and definitions in a code behind, I tried this, which I thought was correct:
var resourceDictionary = new ResourceDictionary()
{
Source = new Uri("ms-appx:///Common/StandardStyles.xaml", UriKind.Absolute)
};
var style = resourceDictionary["ViewPersonTextboxDataStyle"] as Style;
textBlock.Style = resourceDictionary["ViewPersonTextboxDataStyle"] as Style;
So at this point, I can see the style was found in the resource dictionary as style is populated correctly. But assigning into textBlock.Style causes a Catastrophic Exception. So, either I am missing a step or this is incorrect.
Not alot of net information on this.
Ok, thank you Raghavendra, this did point me in the right direction to tell me that things I was trying weren't off base.
What I ended up with is:
style = Application.Current.Resources["ViewPersonTextDataStyle"] as Style;
textBlock.Style = style;
Raghavendra is right, you don't need to use resource manager, and you also don't need to define it in the local XAML. I used the above line to do it by assigning current in every one of my frames anyway.
So with that, my exception was one for an IDIOT (namely me). I should have been using my TEXTBLOCK style not my TEXTBOX style. Assigning a textbox style to the textblock was causing the exception.
Try this:
textBlock.style = this.Resources["ViewPersonTextboxDataStyle"] as Style;
You need not use ResourceDictionary
Edit:
this.Resources refers to Page.Resources (that is in case the ViewPersonTextboxDataStyle is defined in Page.Resources we use this)
Try this one:
textBlock.style = App.Current.Resources["ViewPersonTextboxDataStyle"] as Style;

How to set TextBox.TextAlign = MiddleLeft?

Label.TextAlign is of type ContentAlignment. It allows developer to choose any combination of vertical and horizontal alignments.
TextBox.TextAlign is of type HorizontalAlignment. It only allows Left, Center and Right.
I would like to imitate behavior of ContentAlignment = MiddleLeft for a TextBox.
Basically, I am trying to create a custom MsgBox dialog (MessageBox.Show for non-VB developers). Microsoft's MsgBox seems to have its Label.TextAlign = MiddleLeft automatically set through windows API. I need it to be a TextBox (not a Label), because users should be able to select text and copy to clipboard, partially or in full.
So far I came up with this solution:
Have a TableLayoutPanel, one of its cells will contain the above TextBox.
Set Anchor = Left,Right. This will keep it stretched horizontally.
MeasureText just before showing (example: Form_Load), and adjust TextBox.Height.
Dim size As SizeF = TextBox1.CreateGraphics.MeasureString(
TextBox1.Text, TextBox1.Font, TextBox1.Width)
TextBox1.Height = size.Height
Here is a picture of how it works in action (demo project, not yet a custom MsgBox):
It seems like a rather dirty approach, not resilient to resizing and text changes. These events can be handled accordingly, of course, but it adds to the overall clutter.
Question: Is there a cleaner approach? I am looking for anything that may help implement the above behavior - it does not necessarily have to a be a TextBox.
If you add a WPF ElementHost to your form. Then create a WPF UserControl with a TextBox in it like so:
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="113" d:DesignWidth="234" BorderThickness="2" BorderBrush="Black">
<TextBox x:Name="TextBox1" BorderThickness="0" IsReadOnly="True" Background="{x:Null}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">Hello World</TextBox>
</UserControl>
Then in your code you can set the value like this:
Me.UserControl11.TextBox1.Text = "Some Text Value Here"
Naturally, this assumes that you can use WPF, but on the plus side, you have all the Xaml styling capabilities at your disposal.

Set the maximum chr length of a TextBlock in XAML

How would I set the number of characters a user is allowed to input in a TextBlock in xaml?
Would I do it on the Model or create some sort of custom attribute to handle it?
TextBlock doesn't have a MaxLength, neither does Label. TextBox does. Users cannot input into a TextBlock unless you have modified it.
Is it really a TextBlock you want to limit or did you mean a TextBox? If it is a TextBox you can simply use the MaxLength property.
<TextBox Name="textBox1" MaxLength="5" />
If it really is a TextBlock you are using and somehow allowing a user to input data into it, then switch to use a TextBox. If it is the TextBlock style you are after, you can style the TextBox to look like a TextBlock.
Without creating a custom control, you have a few options.
You could try to size the TextBlock to fit your expected text exactly, but that gets ugly fast trying to account for varying input or different font sizes.
Instead, you can verify the character length of the string to be assigned to the TextBlock.Text property and limit it if necessary.
string s = "new text";
if (s.Length > maxLen)
textBlock1.Text = s.Substring(0, maxLen);
else
textBlock1.Text = s;
Another option is to use the TextWrapping and TextTrimming properties. The following attributes could be added to your TextBlock xaml to add line wrapping and "..." to denote that text exists beyond the size of the TextBlock.
<TextBlock ... TextWrapping="Wrap" TextTrimming="CharacterEllipsis" />
You can use 'TextTrimming' Property of a Textblock. Set TextTrimming = "CharacterEllipsis". You might need to play with Width to manage how many characters you really want to display.
<TextBlock TextTrimming="CharacterEllipsis" Text="{Binding Subject}"/>
Either Set MaxHeight = "SomeHeight" and trim the overflow with
<TextBlock TextTrimming="CharacterEllipsis" Text="{Binding LongText}"
Or Use TextBox like Textblock by setting
<TextBox IsReadOnly="True" Background="Transparent" BorderThickness="0"
MaxLength="100"