Printing a UserControl that contains an Image? - silverlight

I implemented the printer functionnality detailed on this page and I think I stumbled upon a bug in the printing behavior of System.Windows.Printing. I just added an Image in the layout, like this:
<Grid x:Name="documentRoot">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="25"/>
<RowDefinition />
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Image x:Name="logo" Source="/MyProject;component/Image/logo.jpg" Grid.Row="0" Width="265" Height="51" HorizontalAlignment="Left" Margin="0,0,0,15" VerticalAlignment="Bottom" />
<TextBlock x:Name="headerTextBlock" Grid.Row="1" HorizontalAlignment="Center" />
<TextBlock x:Name="bodyTextBlock" Grid.Row="2" TextWrapping="Wrap" />
<TextBlock x:Name="footerTextBlock" HorizontalAlignment="Center" Grid.Row="3"/>
</Grid>
What happens with this code is that whenever multiple pages have to be printed for the first time since the application has started, it will skip the first 2 pages and only print the rest of the pages. If you try to print the same thing again it will print the pages nicely as expected, and forever until you restart the application.
Please note that it has nothing to do with the added RowDefinition, as I tried to just remove the Image element and it worked perfectly fine. It is only when I put the Image element in my UserControl that the printer starts going bonkers.
So I am assuming this is yet another "minor" bug that Microsoft won't ever bother to fix in years, such as this one or some other non-working flags in Powershell that I have had to struggle with in my early days.
This being said, I would be more than happy to be told wrong on this issue. But if I'm not, does anyone have any idea on a workaround?

Found a workaround to my problem: converting the image file into a XAML Canvas using this website, and then copy-pasting the whole Canvas code instead of the Image in my PrintPage.xaml file seems to do the trick. Even though it's quite horrible. I know.

Related

Chinese font renders incorrectly at FontSize less than 22

For the most part, my app renders different character sets without problems. There are, however, certain Chinese characters that render fine when FontSize is 22, but horribly when it is smaller:
This was rendered by the following piece of XAML, which works for both WPF and UWP, with the same problematic results:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox x:Name="textBox" Grid.Row="0" FontSize="21" Text="〾⿰⿱⿲⿳⿴⿵⿶⿷⿸⿹⿺⿻" />
<TextBox Grid.Row="1" FontSize="22" Text="{Binding Text, ElementName=textBox}" />
<TextBlock Grid.Row="2" FontSize="21" Text="{Binding Text, ElementName=textBox}" />
<TextBlock Grid.Row="3" FontSize="22" Text="{Binding Text, ElementName=textBox}" />
</Grid>
The second and fourth line shows how the string is supposed to look; the first and third show garbled versions.
The answer to this somewhat related question suggests setting FontFamily="SimSun", which does indeed cause things to be displayed correctly. My options, then, currently seem to be:
Change the font family from the default "Segoe UI" to "SimSun"
Increase the font size from 15 to 22
Neither option is appealing. Does anyone have other suggestions? Or an explanation for why those particular characters are so troublesome?
Turns out FontWeight="Light" does the trick:
Medium, Normal and SemiLight all fail. Light, SemiBold, Thin, Bold, ExtraBold, Black, ExtraBlack and ExtraLight all solve the problem.
If anyone knowledgeable about this area has something to contribute, I'm still interested, as I have no idea what causes this behavior.

WPF Control, UserControl, Template confusion

Disclosure:
I am new to WPF, about a week into it.
Problem:
I am trying to modify the behavior of a GridSplitter, to make it snap to interesting positions, to show a label (that follows the splitter) with current position, to have a context menu driven from said label, etc. I have prototyped all of this successfully on one gridsplitter in one simple test application, with a combination of XAML and some code behind.
Of note is that because the GridSplitter can't host content, I placed the label in the same grid cell as the splitter so that they move together.
So far so good....
Now I wish to replicate my work so that I can use my new GridSplitter functionality in place of the native control in many locations, and furthermore, I wish to have two variants, a horizontal and a vertical. Sounds like inheritance...create a subclass derived from GridSplitter and add in the additional functionality. But all of the reading I have done leaves me wondering how to go about this, and if this is even possible without starting over again and building my own GridSplitter from scratch?
Ideas welcome. Until then I will resume the fetal position.
Thanks
This answer might help you resolve your issue: How to make GridSplitter to "snap" into another element?
By subscribing to the GridSplitterDragCompleted event, you can insert your logic to snap to "interesting" positions.
You should
create a new control derived from GridSplitter.
subscribe to DragCompleted event to implement snapping functionality like DLeh mentioned.
add a few new properties for Label , ContextMenu etc.
supply a style for your new control.
This answers how to place content in the splitter
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="40" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Row 0" Background="Orange"/>
<!--<GridSplitter Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch" Height="20" Background="Purple"/>-->
<GridSplitter Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch">
<GridSplitter.Template>
<ControlTemplate TargetType="{x:Type GridSplitter}">
<TextBlock Text="TextBlock splitter" Background="Yellow" FontWeight="Bold"/>
</ControlTemplate>
</GridSplitter.Template>
</GridSplitter>
<Button Grid.Row="2" Content="Row 0" Background="Salmon"/>
</Grid>

Silverlight Viewbox not working with IE9

I'm developing a silverlight application for use on small and large monitors,
I had the code working perfectly, my main page contains a viewbox and all controls are children of that viewbox and they did scale correctly, i've been testing on IE9 the whole time.
Recently VS2010 crashed which is does quite frequently when dealing with silverlight, ever since this crash IE9 refuses to display my Viewbox correctly.
Chrome / Opera / Firefox / Safari all have the correct behaviour but not IE9 anymore.
When the application starts, its initial size is correct but after a few seconds it seems to break out of the viewbox.
MainPage.xaml
<Viewbox MinHeight="600" MinWidth="800">
<Grid Width="1024" Height="768" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="25">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<sdk:Frame Grid.Row="0" Style="{StaticResource FrameStyle}" Source="{Binding SelectedPage.PageUri}" NavigationFailed="Frame_NavigationFailed" />
<Border Grid.Row="1" BorderBrush="#FFD4D2D2" Background="White" BorderThickness="1 0 1 1">
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
...
</StackPanel>
</Border>
</Grid>
</Viewbox>
the following images show what the page looks like in IE and Chrome.
Chrome at roughly 800x600 (Correct)
IE at roughly 800x600 (Incorrect)
Both are using the same XAP file the app was never run on chrome before so it shouldnt have been cached, either way the cache was cleared before testing.
Has anyone had a problem similar to this before? are there any work arounds to make this work on IE?
Is this a bug with silverlight or IE?
Any help would really be appreciated
Thanks
Have you control-mouse-wheel zoomed the IE display by any chance?
You have specified minimum display sizes on the ViewBox, so you should probably contain that ViewBox in a ScrollViewer in case it does not fit.

Using the WPF WrapPanel to Wrap and Right Align Text - WPF Bug?

I am attempting to use the WrapPanel and two TextBlocks to append an asterisk (*) to the left side of some text, allow the text to wrap, and force the text to be right aligned. I have successfully done so by creating a WrapPanel with the FlowDirection set to RightToLeft and adding my text, followed by the asterisk. However, if the text I use happens to have any non-alphanumeric characters at the end of the line it is inexplicably forced to the front of the line. I find this behavior to be very strange. I think it must be a bug in WPF and not intended behavior.
Example with Text = Normal Text (Other Text) :
Expected:
* Normal Text (Other
Text)
Actual:
* Normal Text (Other
(Text
Feel free to use the following sample code to recreate the issue for yourself. Simply put this in a window with Height and Width = 100, then type "Normal Text (Other Text)" in the TextBox. Or, set the Height and Width to anything you like and write enough text that it is forced to wrap the text, then add punctuation to the end.
Sample Code:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Name="input" />
<WrapPanel Grid.Row="2" FlowDirection="RightToLeft">
<TextBlock Text="{Binding ElementName=input, Path=Text}" TextWrapping="Wrap"/>
<TextBlock Text="*" Margin="0,0,3,0"/>
</WrapPanel>
</Grid>
So, my question(s).
Is this a bug, or is this intended?
If this is a bug, should I submit it to Microsoft in some way? How?
Since starting this post, I have decided to put the two TextBlocks in a two column grid instead. With the non-asterisk containing TextBlock configured to use a Right TextAlignment I meet all my requirements anyway. Still, I found this to be an interesting issue.
Try this instead:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Name="input" />
<WrapPanel Grid.Row="2" HorizontalAlignment="Right" >
<TextBlock Text="*" Margin="0,0,3,0"/>
<TextBlock Text="{Binding ElementName=input, Path=Text}" TextWrapping="Wrap"/>
</WrapPanel>
</Grid>
FlowDirection is for use to support languages that are read that from right to left. Since I don't know the rules for languages like that I'm not going to pretend to understand why you are seeing what you are, or if it is a bug. That said I know that changing the FlowDirection isn't the correct way to handle right aligning left to right languages and you should use HorizontalAlignment instead.
(For future reference you submit bugs to Microsoft through the Connect site)
In .Net 4 (WPF4) runs are bindable, so you can try something like so:
<TextBlock TextAlignment="Right" TextWrapping="Wrap">
<Run Text="*" /><Run Text="{Binding ElementName=Input, Path=Text}" />
</TextBlock>
The two Run elements are on the same line because any type of whitespace between the tag boundaries will cause a space to appear between the two runs. (As HTML does.)

Silverlight: why png-image is not displaying?

In Silverlight 4 application there are few images, both are displayed correctly in design mode, one is displayed correctly in run-time also:
<Image Height="180" Width="149" Source="../Picts/Field.png" />
Another one is not displayed in run-time:
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5" Visibility="{Binding SquadSavedVisibility, Mode=OneWay}">
<Image Source="..\Picts\ok.png" Width="16" Height="16" />
<TextBlock Text=" It is saved" Foreground="Green"/>
</StackPanel>
Why? And how to get it displayed?
Any thoughts are welcome. Thanks.
It's all in your slashes, VS Design time doesn't mind you using "..\Picts\ok.png", but Silverlight runtime wants to see "../Picts/ok.png". In other words, your slashes matter.
I had a similar problem with images showing in design-time, but not at runtime. Mine was using a pack URI so I wanted to post that fix as well:
Does not work at runtime, does work at design-time:
<Image Source="mydllname;component/Images/logo.png" />
Works at both design and runtime:
<Image Source="/mydllName;component/Images/logo.png" />
Note the extra '/' before the Pack URI starts.
You can try to hook into the Image.ImageFailed Event. For examples and more explanation you look into this page:
http://msdn.microsoft.com/en-us/library/system.windows.controls.image.imagefailed%28v=VS.95%29.aspx

Resources