Change WPF DocumentViewer viewing mode - wpf

Hello I've got the problem that when I just add a <DocumentViewer x:Name="docViewer" /> I get a toolbar what allow me to change the viewing mode of the XPS document like here:
But how can I do this programmatically?
I cant find any property that do this and the MSDN and Google say nothing about this feature.
Then I found the FlowDocumentReader control what have a ViewingMode property, however it can't display a XPS document. I can't believe that it is not possible to do this, I mean that function is available by default, I just want to change it programmatically...

DocuementViewer didn't exposed any property to change the ViewMode. But in case you want to change the ViewMode programmatically, you can execute the commands exposed by DocumentViewer which are as -
DocumentViewer.FitToWidthCommand
DocumentViewer.FitToHeightCommand
DocumentViewer.FitToMaxPagesAcrossCommand
You can bind these commands to your custom button or programmatically you can change the ViewMode. Suppose you have a DocuementViewer named documentViewer, you can call these methods on this instance like this -
documentViewer.FitToWidth()
documentViewer.FitToHeight()
documentViewer.FitToMaxPagesAcross()

Related

How to get the old value of a TrackBar without a custom control or separate field?

I am trying to get the old Value of a TrackBar when the Scroll event gets raised. I could do this by creating a separate field and storing that value whenever it changes, or overriding the event in a custom control however, I am already using the built-in TrackBar and I'd prefer to not have to redesign my forms.
The Microsoft documentation does not seem to have any information on this and the EventArgs parameter on the OnScroll and OnValueChanged methods seem pretty generic.
Is there a way to achieve this and how?
Sources:
Microsoft Documentation: TrackBar.OnScroll Method
Microsoft Documentation: TrackBar.Scroll Event
How to override method and event in WinForm UserControl in C#?
Microsoft Documentation: TrackBar.ValueChanged Event
Microsoft Documentation: TrackBar.Value Property
Microsoft Documentation: TrackBar.OnValueChanged Method
I believe there isn't a built-in way to achieve what you're looking for.
To address your concern about having to replace all the TrackBar controls on a form with the derived custom control, here's how I usually do it:
Create your control class which inherits the TrackBar control.
Open your FormName.Designer.cs or FormName.Designer.vb file.
Click Ctrl+H to open the "Find and replace" window.
Enter System.Windows.Forms.TrackBar in the "Find" field, and YourNameSpace.CustomTrackBar in the "Replace with" field.
Click "Replace all" (You can do it one by one if you're afraid to mess something up).
Rebuild your project.
Hope that helps.

Button custom attributes for windows forms

I need to add a secondary Id or even some custom text to controls like Button, Textbox, ListBox etc., so that i can later use it for programmatic purposes and should not be displayed to user.
I can do this in ASP.net using Attributes property for almost any control, but when I checked with windows forms I found it doesn’t have this property, can I find any other alternatives in windows forms?
You can use Control.Tag property for this.
myButton.Tag = whatever;
myTextBox.Tag = whatever;
...
Use the Control.Tag property. Source: MSDN
"A common use for the Tag property is to store data that is closely associated with the control."
This sounds like what you're after.

Make Custom WinForms Control Work with Narrator (Accessibility)

I have a custom list control derived from Control class.
I need to make it accessible to people with disabilities through MSAA (Microsoft Active Accessibility).
So far I understand that I need to create class that inherits from ControlAccessibleObject and then return its instance in Control.CreateAccessibilityInstance method override.
The problem is that I have implemented this and it seems not work with Windows Narrator tool.
For example, when I click on an item in standard ListView, the Narrator speaks out the selected item text.
But when I click on item in my control, nothing happens (although the item text is requested in my ControlAccessibleObject implementation)
I thought I need to implement IAccessible as well, but I looked on .NET refrence source code and the ListView does not implement this interface. I thought maybe this is implemented in the wrapped Win32 control, so I took a look on similar control - DataGridView - but this does not implement IAccessible as well.
DataGridView have accessibility support, but although I copied all the important code of DataGridViewAccessibleObject, it still does not work in my control.
Do anyone have more experience with custom control accessibility in WinForms?
Okay, I found it: The Control.AccessibilityNotifyClients method does the magic. One have to override this method in a derived control.
However, to make screen readers speak the text, I had to call:
AccessibilityNotifyClients(AccessibleEvents.Focus, index);
AccessibilityNotifyClients(AccessibleEvents.Selection, index);
Here the index is an index of newly selected item.
I found this code in the .NET reference source of CheckedListBox. When I used Focus or Selection event solely, the screen reader have not reacted. The spoken text also depend on the AccessibleObject state that corresponds to a newly selected item.

WPF Documentviewerbase.Print. Remove dialog box

I am using WPF and the DocumentViewer to display a document. However, when I use DocumentViewerBase.Print, it gives the "standard" Windows dialog box asking me to choose a printer with the default already selected. How can I get rid of this? I just want to use a Print method that will automatically start printing with no other prompt in between.
I believe in order to do this you would need to write your own printing routine e.g. by overriding OnPrintCommand method of the DocumentViewer. The reason is that default implementation is using PrintQueue.CreateXpsDocumentWriter method to create XpsDocumentWriter object and shows a PrintDialog in order to define its properties. For more details check the DocumentViewerBase.OnPrintCommand with reflector

WPF Handle links inside FlowDocument

I'm building simple dictionary application using WPF.
I'm using MVVM pattern, databinding and FlowDocument to display words to user.
UI consists of 2 controls: ListBox and FlowDocumentScrollViewer.
Dictionary data comes from XML file, format of this string may look like this
<b>Word</b> - Some description. Another <i>description</i>. Reference <ref id="123">related word</ref>
The order of HTML and reference elements is not defined.
I parse HTML string, make it into XAML, then from XAML I create FlowDocument object and bind it to Document property of FlowDocumentScrollViewer control.
The problem arises when I need to link ref. elements. (my requirement is when user clicks on reference link, referenced word is selected in ListBox control and shown in FlowDocumentScrollViewer)
My question is there a way to dynamically create "hyperlink"-style controls (with event or commands attached) that would take user to referenced word in dictionary.
Is it possible to solve this problem at all?
There is the Hyperlink text element that has a Command property and a Click event. It behaves pretty much like a button, but it is used inside the FlowDocuments. You can use either method to achieve what you are after, but I tend to prefer Commands. Especially if you are implementing this using the MVVM pattern as you have tagged...

Resources