I am using C# WPF (trying to use MVVM) with GalaSoft.MvvmLight. In my View I have a list box
<ListBox x:Name="OutputListBoxWindow"
Grid.Row="5"
Grid.Column="0"
Grid.ColumnSpan="8"
Margin="5,5,5,5"
Height="Auto"
Background="Black"
Foreground="LimeGreen"
ItemsSource="{Binding OutputListBox}"
SelectedIndex="{Binding OutputListBoxIndex}"
IsEnabled="{Binding Enable, Mode=OneWay}"
AutomationProperties.IsColumnHeader="False">
I also have a print button
<Button
Grid.Row="0" Grid.Column="6"
Command="{Binding PrintCommand, Mode=OneWay}"
CommandParameter="{Binding ElementName=OutputListBoxWindow, Mode=OneWay}"
Content="Print"
Margin="20,15,20,15"/>
I have put what I believe to be the Visual (the item I want to print) in as a CommandParameter.
In my ViewModel I have a RelayCommand not ICommand
public RelayCommand<Visual> PrintCommand
{
get
{
return new RelayCommand<Visual>(vdata =>
{
PrintDialog myPrintDialog = new PrintDialog();
if (Convert.ToBoolean(myPrintDialog.ShowDialog()))
{
myPrintDialog.PrintVisual(vdata, "listbox contents Printing.");
}
});
}
}
I put a breakpoint in at the line with PrintVisual and I can see that I have hold of the ListBox as it knows how many lines of data I have. If I continue I get the print dialog. I then choose print to PDF and give it a name. It does create a file but what I get appears to be the main window minus the ListBox. I think at the bottom of the PDF I get just a few pixels of the ListBox as shown below.
[![PDF printout][1]][1]
Thinking it was a ListBox issue I tried to print a TextBlock but now I get a blank. Not sure if it helps but in the Print dialog I choose to have A4 paper etc. The print I get is not A4 and seems to be the size of something else. I was expecting an image inside the A4. What have I missed here please.
Printing PDF from Notepad works so the PDF print driver is working
Update: This is what I think is happening. If I put a name in the the window tag and then print that I get the whole window in full colour. If I put a name in a control and try and print that I get the issue. Looking at it further I saw that I know I have hold of the control and I know its size so I know what size I will print. Here is the issue instead of picking up the correct corner to start to print from it picks up the incorrect corner and start to measure from that. This is why in my image above the top is blank and the bottom has just the start of my list box. I tried this with many different controls and they all print but from an entirely different part of the window. At this stage I have no idea how to fix this.
[1]: https://i.stack.imgur.com/b8GmQ.png
Related
Im trying to use a Snackbar to display information, but the text i want to display is too long to fit inside the small window horizontally, so the text isn't shown completely:
An automatic line break would be super ok.
Setting it manually wouldn't work, because it needs to display different messages.
I actually got it working by setting the property FontFamily:
FontFamily="{DynamicResource MaterialDesignFont}"
To make it working for arbitrary number of lines, just delete MaxHeight setter for ContentPresenter in SnackbarMessage Style:
MaxHeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type wpf:Snackbar}}, Path=(wpf:SnackbarMessage.ContentMaxHeight)}"
I am fairly new to WPF and have two questions?
Question #1:
From my XAML snip below, by button "btnRed" word's fine with my code behind. When the button is clicked, the proper view is display. However, how does one perform the same thing "programmatically"? Hence, my next question.
Question #2:
I am not sure how to make a "textbox" and "button" work together to perform the same action. What I'm trying to do is this. (1) I would like the textbox to be linked to the "DataContext" of the button, "btnDisplayView". (2) so when I type in, say, "RedView" into the textbox and click the button, the correct view is displayed.
My long term goal is to have a database, with a couple of tables. A table for "MenuItems" and a table for "Views". Instead of using buttons, I'll use the menu control. Then once a menu item is selected, it would display the correct view.
But for now, I'm startings small and trying to keep it simple:
--------- WPF - XAML START ---------------------------
<StackPanel Grid.Column="0" Orientation="Vertical">
<TextBox x:Name="txtDisplayView" Height="23" Margin="5" TextAlignment="Center"/>
<Button x:Name="btnDisplayView" Content="Display" Margin="5" Click="btnDisplay_Click"/>
<Button x:Name="btnRed" Content="Red" Margin="5" DataContext="RedView" Click="Red_Click"/>
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Vertical">
<ContentControl Margin="5" Content="{Binding}"/>
</StackPanel>
-----------WPF - XAML END -------------------------
If someone could show me how to get this to work, it would help me move my project in the right direction and would be greatly appreciated.
What you need here is:
Create a property in your DataContext that represents the selected item
Bind that property to your TextBox element
Now, you have two options. One is "WPF Friendly" and the other is more Windows Forms-ish:
Create a command (take a look at this article) that reads a parameter, which will be binded to the property you created before
On the Click event, you can read the binded property value
I personally prefer the first solution. Why? Because when you change it to a Menu, for example, your work will be only to populate the menu with your list items (the MenuItem class also has a Command property, so the implementation is the same as with a Button). You will only need to change the source!
I am trying to create an application to take notes for windows phone 8.1
I want to give the user,a notebook type of feel.
For this I have created the UI for notes, the XAML is:
<Grid Margin="0,12.333,0,-0.333">
<Grid.Background>
<ImageBrush ImageSource="Images/notebookpaper.jpg"/>
</Grid.Background>
<TextBox TextWrapping="Wrap" Background="{x:Null}" Text="" BorderBrush="{x:Null}" HorizontalAlignment="Left" Margin="60,96,0,0" VerticalAlignment="Top" Height="480" Width="340" BorderThickness="0" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus" FontFamily="Arial" TextChanged="TextBox_TextChanged" AcceptsReturn="True" FontSize="24.8"/>
<TextBlock Text="Date : " HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="246,10,0,0" Height="20" Width="59"/>
</Grid>
The image notebookpaper.jpg looks like this:
When user types in the text in text box, it looks like:
The problem is that, some characters appear a little above the line, some exactly on the line etc. which looks odd. Also, when I try to scroll, UI appears as:
The text appears striked out, as only the text scrolls and not the background image.
Also I want to be able to provide user a list of 5-6 fonts out of which they can select which one to use for typing the notes.
What should I do, so that the text appears properly aligned and text scrolls properly.
Is there any other way to do this ?
It looks like you have two problems:
Varying line height
Scrolling doesn't match the lines
To solve the first problem, you can probably work with TextBlock.TextLineBounds, talked about a bit in this MSDN blog post and the TextLineBounds enumeration documentation. This only seems to apply to TextBlocks, so you might have to swap between a TextBlock and TextBox as users edit their text.
To solve the second problem, the TextBox styles and templates page has a lot of helpful info. It looks like you can make your ImageBrush the background of your control by overriding TextBoxButtonBackgroundThemeBrush. If that doesn't work when focused, you may have to take the entire template given on the linked page and edit it to put your image in the background (there's a lot of XAML, but you should just be able to put your image in BackgroundElement or just before it).
If it still doesn't scroll, you can try setting ScrollViewer.Background instead; if that doesn't work, you'll need to handle the ScrollViewer.ViewChanging or ScrollViewer.ViewChanged events (probably by overriding it) so that it you can transform the background image by the amount of pixels the scrollviewer has moved.
You can also find the ScrollViewer in your code-behind (and skip dealing with the template) by using VisualTreeHelper. This would allow you to set the background of the ScrollViewer and/or subscribe to its events. This however is more brittle than the other methods and is usually a last resort.
I am trying to create custom menu which consists of text at least 2 textblock in one rectangle, which will act as button, For that purpose, May be converting the text into Image may be good idea to implement and serve my purpose to. so I want and Idea or code to create image during runtime, any suggestion ? If you have any sample code or link to refer??
if by "2 textblock in one rectangle" you mean text in two lines then try this
<Button>
<StackPanel>
<TextBlock Text="ABC"/>
<TextBlock Text="DEF"/>
</StackPanel>
</Button>
I am working with some RichTextBlock objects that contain InlineUIContainer elements. I would like to be able to select and copy all of the text including the text contained in the InlineUIContainer.
Currently, when I select all of the text in the block, the text contained in the InlineUIContainer objects are skipped.
Here is an example of what I'm creating:
<RichTextBlock IsTextSelectionEnabled="True">
<Paragraph FontSize="20">
<Bold>This text is selectable</Bold>
<InlineUIContainer FontFamily="Global User Interface">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="11" VerticalAlignment="Top" Margin="0,0,-1,0">Super Script Text</TextBlock>
<HyperlinkButton ClickMode="Release" Style="{StaticResource NoMarginHyperlinkButtonStyle}">
Link
</HyperlinkButton>
</StackPanel>
</InlineUIContainer>
This text is also selectable
</Paragraph>
</RichTextBlock>
If I select all of the text from this piece of Xaml and copy/paste it in NotePad, I don't get the Super Script Text or the Link text.
Is there any way to get all of the text selected?
This is because HyperlinkButton is not part of the document API and in fact a UIElement wrapped in InlineUIContainer. There are 2 ways to handle this.
Switch to Windows 8.1 and Hyperlink which inherits from TextElement and copy will work just fine.
This is the hard way, if you must support this in Windows 8.
Remove the default Context menu items for the RichTextBlock and replace with your own Copy Command. Which should get the 2 TextPointers i.e. RichtextBlock.SelectionStart and RichTextBlock.SelectionEnd
Now with WPF we could get a TextRange within this range but winRT does not expose it, so you will need to do it in your code...
Get all the block within the RichTextBlock, and iterate through each to check if it's ContentStart and ContentEnd is within the RTB.SelectionStart and RTB.SelectionEnd if so then add them to a list.
Now it should be easy to extract all the Runs and Bold/Italics from this list and any InlineUIContainers hosting HyperlinkButtons.
2 This is not really a good way to go as it will be hard to allow for margins etc. on Paragraphs etc.