Visual Studio 2010 XAML layout editor not WYSIWYG - wpf

When I'm creating WPF layouts in Visual Studio 2010 I just drag and drop controls from the Toolbox onto the default element, typically a Grid. When I do this the spacing shown in Visual studio does not match what's displayed at runtime. For example, here are the visual editor and runtime results, respectively, for two layouts . . .
. . . and . . .
... notice the spacing is altered.
Here's the XAML that was generated for the two examples . . .
<Button Content="ProjectPattern" Height="23" HorizontalAlignment="Left" Margin="12,296,0,0" Name="butProjPattern" VerticalAlignment="Top" Width="117" Click="butProjPattern_Click" />
<TextBlock Height="22" HorizontalAlignment="Left" Margin="135,0,0,141" Name="ResultProjPattern" Text="(result)" VerticalAlignment="Bottom" Width="41" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="175,295,0,0" Name="TextBlockPattern" Text=" (pattern file)" Padding="4" VerticalAlignment="Top" Width="135" Background="#FFF1F3FF" />
<Button Content="...browse" Height="23" HorizontalAlignment="Right" Margin="0,294,814,0" Name="buttonPatternBrowse" VerticalAlignment="Top" Width="54" Click="buttonPatternBrowse_Click" />
...and...
<Button Content="Display Cross" Height="24" HorizontalAlignment="Left" Margin="622,108,0,0" Name="buttonCross" VerticalAlignment="Top" Width="117" Click="buttonCross_Click" />
<TextBlock Height="24" HorizontalAlignment="Right" Margin="0,107,344,0" Name="ResultCross" Text="(result)" VerticalAlignment="Top" Width="100" />
<Button Content="Display Diamond" Height="23" HorizontalAlignment="Left" Margin="622,138,0,0" Name="butDiamond" VerticalAlignment="Top" Width="117" Click="butDiamond_Click" />
<TextBlock Height="23" HorizontalAlignment="Right" Margin="0,138,344,0" Name="ResultDiamond" Text="(result)" VerticalAlignment="Top" Width="100" />
<Button Content="DisplayFullField" Height="24" HorizontalAlignment="Left" Margin="622,165,0,0" Name="butFullField" VerticalAlignment="Top" Width="117" />
<TextBlock Height="24" HorizontalAlignment="Right" Margin="0,164,344,0" Name="ResultFullField" Text="(result)" VerticalAlignment="Top" Width="100" />
I added button handlers, text content, etc, but the layout is strictly what was generated by the Visual Studio layout editor. One thing I noticed is that even though it's drag-and-drop from the Toolbox onto the Grid, Visual Studio is inconsistent about whether it chooses "Left" or "Right" for its HorizontalAlignment for different elements.
The container element is just a default Grid in a default Window - it can minimize but not resize . . .
<Window x:Class="Caller1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="1200" ResizeMode="CanMinimize">
<Grid Height="460">
The result is that I have to waste lots of time going between the editor and running my app to "tweak" my layout. What prevents the "WYSIWYG" layout editor from matching the runtime layout and how can I get them to correspond?
Thanks in advance.

I can't see your images (dumb work firewall), but I can tell by your XAML that you are using the Grid wrong. I understand that this may be the fault of the editor, but seriously, you shouldn't be setting the Width and Height properties on all your elements.
If you want a fixed size for the Grid's children, set up some RowDefinitions and ColumnDefinitions and set those sizes. Your layout is much easier to work with (and will probably be more accurate in the designer) if you do this. So, your first Grid would look like this:
<Grid Margin="12,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="117" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="138" /> <!-- This is your desired width + horizontal margins -->
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button x:Name="butProjPattern"
Grid.Column="0"
Content="ProjectPattern"
VerticalAlignment="Center"
Click="butProjPattern_Click" />
<TextBlock x:Name="ResultProjPattern"
Grid.Column="1"
Margin="6,0,0,0"
Text="(result)"
VerticalAlignment="Center" />
<TextBlock x:Name="TextBlockPattern"
Grid.Column="2"
Margin="3,0,0,0"
Padding="4"
Text=" (pattern file)"
Background="#FFF1F3FF" />
<Button x:Name="buttonPatternBrowse"
Grid.Column="3"
Content="...browse"
Margin="22,0,0,0"
VerticalAlignment="Center"
Padding="2,1"
Click="buttonPatternBrowse_Click" />
</Grid>
The other thing I want to note is that after I put this in Kaxaml, which is great for rapid prototyping of your XAML layouts, I noticed that you should really just be using a StackPanel. The first grid could really be this much simpler implementation (you will have to set the Width on a couple of your items to match the layout):
<StackPanel Margin="12,0,0,0" Orientation="Horizontal">
<Button x:Name="butProjPattern"
Content="ProjectPattern"
Width="117"
Click="butProjPattern_Click" />
<TextBlock x:Name="ResultProjPattern"
Margin="6,0,0,0"
Text="(result)"
VerticalAlignment="Center" />
<TextBlock x:Name="TextBlockPattern"
Margin="3,0,0,0"
Padding="4"
Width="135"
Text=" (pattern file)"
Background="#FFF1F3FF" />
<Button x:Name="buttonPatternBrowse"
Content="...browse"
Margin="22,0,0,0"
VerticalAlignment="Center"
Padding="2,1"
Click="buttonPatternBrowse_Click" />
</StackPanel>

This answer is not directly an answer to your actual question, but instead a suggestion to avoid your problem.
You will often get UI control placement problems when you use exact control positioning with the Margin property. It is a good procedure (and in WPF especially) to allow controls to place and/or space themselves using the various Alignment options available. I have always managed to get the desired layout using this method.
For example, had you used a Grid or StackPanel to arrange your controls, you would not have come across this problem. Many WPF developers that I know don't even bother using the designer in Visual Studio because of the problems that you mentioned... after dragging and dropping a control and then going to the XAML to remove unwanted values, it often works out quicker to simply type the XAML myself.
If you really want to know why the Visual Studio designer... well, er... doesn't work properly, you may have more luck asking on the Microsoft Developer Forum, where you question might actually get seen by a Microsoft employee.

Related

Control blocking input to lower control

Hopefully this is a WPF issue and not a esri issue. I have an ESRI map control, and I am placing a control on top of the map. When I do this, the map no longer receives input(I can't move around or click anything). If I modify the z-index of the top control to place it behind the map, the map works fine again.
<Grid Name="MapGrid" DockPanel.Dock="Top" >
<Grid Name="MapGrid" DockPanel.Dock="Top" >
<esri:MapView x:Name="MainMapView" Panel.ZIndex="0" KeyDown="MyMapView_KeyDown" MapViewTapped="MyMapView_MapViewTapped" Map="{Binding Source={StaticResource MapResource}}" WrapAround="True" Grid.Row="0" Grid.Column="0" Initialized="MyMapView_Initialized" >
</esri:MapView>
<Expander Header="Properties" ExpandDirection="Right" Panel.ZIndex="-1">
<ItemsControl Background="Transparent" Height="700" Width="500" HorizontalAlignment="Left" VerticalAlignment="Top" Name="FeaturePropertyRegion" cal:RegionManager.RegionName="FeaturePropertyRegion" />
</Expander>
</Grid>
This code works, but if I raise the ZIndex of the Expander pannel, the map no longer receives input. I am assuming the issue has to do with the visual tree of WPF, and how input cascades down. Any ideas on what the issue could be? Thanks.
EDIT
The issue seems to be with the expander, as the map works if I remove the expander and just have the ItemsControl.
Try :
<Expander IsHitTestVisible="False">
<ItemsControl />
</Expander>
It doesn't seem to be a problem with extender, which would be very wired if it did
I tried this :
<Grid>
<Button />
<Expander HorizontalAlignment="Left" ExpandDirection="Right" VerticalAlignment="Top">
<Rectangle Fill="Red" Stretch="Fill" Width="525" Height="350"/>
</Expander>
</Grid>

Checkbox in my windows 8.1 app won't activate in emulator

I know this is going to sound really stupid but is there anything I'm missing because I have a simple page in my windows 8.1 phone app with a checkbox and a button. I can click on the button just fine in the emulator but I can't get the checkbox to check or uncheck at all. I feel really stupid for not being able to do something this simple. Am I missing something here?
<Grid>
<TextBlock Name="lblTermsofAgreement"/>
<CheckBox Name="chkAcceptTerms" Checked="chkAcceptTerms_Checked" Click="chkAcceptTerms_Click" Unchecked="chkAcceptTerms_Unchecked" HorizontalAlignment="Center" Content="I accept the terms of agreement" VerticalAlignment="Center" IsEnabled="True"></CheckBox>
<TextBlock Name="lblTermsError" FontFamily="Red" Text="" />
<Button Name="btnAccept" Click="btnAccept_Click" Content="Accept" HorizontalAlignment="Center" VerticalAlignment="Bottom"></Button>
</Grid>
All the controls are in row 0 column 0 for the grid so there was a control on top of it preventing it from getting checked. I would either add rows to the grid and set there rows or put the controls in a stack panel.
<Grid>
<StackPanel>
<TextBlock Name="lblTermsofAgreement"/>
<CheckBox Name="chkAcceptTerms" HorizontalAlignment="Center" Content="I accept the terms of agreement" VerticalAlignment="Center" IsEnabled="True"></CheckBox>
<TextBlock Name="lblTermsError" FontFamily="Red" Text="" />
<Button Name="btnAccept" Content="Accept" HorizontalAlignment="Center" VerticalAlignment="Bottom"></Button>
</StackPanel>
</Grid>

Calendar control width is too large at initialization of usercontrol

I have used calendar control and its display mode is year when my user control is loads then calender width s too large and doesn't appear when width is set .but when is select any month then width is set as it should
i m not getting what is the issue behind it
can anyone help me quickly???
Here is my code and what ever selected month will appear in textbox's text property and on click event of button my calendar control is set to visible
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="HorizontalHorizontalAlignment="Left "Margin="4">
<Label Content="Month: " HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="15" FontWeight="DemiBold" Margin="4"/>
<TextBox Height="23" HorizontalAlignment="Stretch" Name="SelectedMonthTextBox" VerticalAlignment="Top" Width="130" Margin="4" LostFocus="SelectedMonthTextBox_LostFocus" SelectionBrush="#FF767879"/>
<Button Content=".." Height="20" Width="20" Margin="4" Name="button" Click="button_Click" />
</StackPanel>
<Calendar Grid.Row="1" Grid.Column="0" Margin="50,-8" Panel.ZIndex="100" Name="calendar1" DisplayModeChanged="calendar1_DisplayModeChanged" DisplayDateChanged="calendar1_DisplayDateChanged" HorizontalAlignment="Left" DisplayMode="Year" Style="{StaticResource CalendarStyle}" Focusable="False" HorizontalContentAlignment="Left" VerticalAlignment="Top" />
i took a look your code and from information you provided so far, there is no any change a you describe. I believe that you should check following:
What happens in calendar1_DisplayModeChanged
How defined your style: StaticResource CalendarStyle
If it possible please post them here. I will try to help as soon as possible :)

WP7 An Image over the full display

I am developing an application where i want to browse images like native WindowsPhone form.
I have used Pivot control. Everything works, but there is one unwanted thing. The image does not fill all display area. There is a gap on the top of page. I have set margin and padding everywhere where it is possible. And the result is still the same. :(
Here is my XAML code:
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid
x:Name="LayoutRoot"
Background="Transparent"
Margin="0">
<toolkit:PerformanceProgressBar
VerticalAlignment="Top"
HorizontalAlignment="Left"
IsIndeterminate="{Binding IsBusy}"
Visibility="{Binding IsBusy, Converter={StaticResource BoolToVisibilityConverter}}"
/>
<controls:Pivot
x:Name="PhotoPivot"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
IsHitTestVisible="True"
ItemsSource="{Binding Photos}"
Margin="0"
Padding="0"
>
<controls:Pivot.HeaderTemplate>
<DataTemplate/>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate >
<controls:PivotItem
x:Name="PhotoPivotItem"
Margin="0"
>
<Image
x:Name="PhotoPicture"
Source="{Binding}"
Stretch="Uniform"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="0"
/>
</controls:PivotItem>
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
</Grid>
Are you talking about the system tray?
To remove the system tray use the following code:
shell:SystemTray.IsVisible="False"
Update:
I'm not real sure why that extra space is there.
I created a simplified version of your example and the only thing I could think of is to use negative margins. There is probably a better solution that I am just overlooking, but for now you can just use the following:
<controls:PivotItem x:Name="PhotoPivotItem" Margin="0,-10,0,0">
Although, from your posted image, it looks as if you have a bigger gap than I did, so you might need to decrease the margin.

Silverlight and Unexpected Font Sizes

Someone please teach me to fish here...
I'm just learning Silverlight and have ran into a few situations where the font size actually used is drastically different than I would expect. There's probably something conceptual that I'm missing.
Case A
In one instance, I have defined a user control that presents a Label to show text. If one clicks on the label, the label (that is in a stack panel, in the user control) is replaced with a TextBox. When used at the top of a page (as in the example below with lblName) the label text is very small (around 8 points). When clicked on, the text box that replaces the label uses the specified fonts size. That same user control, used in different parts of the app, uses the same font for Label and TextBox.
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="33" />
<RowDefinition Height="267*" />
</Grid.RowDefinitions>
<StackPanel Height="Auto" HorizontalAlignment="Left" Name="stackPanel" VerticalAlignment="Top" Width="Auto" Grid.Row="1" />
<my:EditLabel Height="33" HorizontalAlignment="Left" x:Name="lblName" VerticalAlignment="Top" Width="Auto" FlexText="{Binding Name, Mode=TwoWay}" FontSize="20" MinHeight="24" />
</Grid>
Case B
I'm using the LiquidMenu.Menu control to pop up a menu when a button is pressed. The font looks huge compared to the rest of my page (maybe 36 points?). I tried forcing it to a very small by explicitly setting it to 8pt, but that had no effect.
<Grid x:Name="LayoutRoot" Background="{x:Null}">
<StackPanel x:Name="labelStackPanel" Orientation="Horizontal">
<TextBlock Height="24" HorizontalAlignment="Left" Name="labelText" VerticalAlignment="Top" Width="200" Text="(Value Goes Here)" />
</StackPanel>
<liquidMenu:Menu x:Name="popupMenu" Canvas.Left="40" Canvas.Top="40" ItemSelected="MenuList_ItemSelected" Visibility="Collapsed" Height="Auto" FontSize="8">
<liquidMenu:MenuItem ID="delete" Icon="Images/Delete10.png" Text="Delete" Shortcut="Del" />
<liquidMenu:MenuItem ID="exclusive" Icon="" Text="Exclusive" Shortcut="Ctrl+E" />
<liquidMenu:MenuItem ID="properties" Icon="" Text="Properties" Shortcut="Ctrl+P" />
</liquidMenu:Menu>
</Grid>
Answers to these specific issues are great, a new way to think about this type of issue so that I understand how to control font size is better.

Resources