White spaces appear around the document to preview - wpf

I have WPF Application which use printing.
I have class inherite from "DocumentPaginator"
class ReportPaginator : DocumentPaginator
{
private Size pageSize;
public override IDocumentPaginatorSource Source
{
get { return null; }
}
public override bool IsPageCountValid
{
get { return true; }
}
public override int PageCount
{
get { return pageCount; }
}
public override Size PageSize
{
get { return pageSize; }
set
{
if (value != null)
{
pageSize = value;
CalculatesPage();
}
}
}
public override DocumentPage GetPage(int pageNumber)
{
// some code.
}
}
When I get FixedDocumentSequence from this paginator to preview the document before printing.
the "pageSize" property did NOT applied for this DocumentPage, and there is white spaces around the pages
How can I solve this problem

Just for the case if some one had the same problem of this.
My problem was in the my own code [specifically the part that generate the Page in the GetPage() method], I was generating white spaces around the page itself when implement GetPage method.
if you are having same problem, may be double-checking GetPage method may be helpful.

Related

How to pop to root

I am writing a WPF application with MvvmCross. I have a custom view presenter that I want to use so that I can pop multiple view models in 1 shot. Here is my view presenter:
public class ViewPresenter : MvxWpfViewPresenter
{
ContentControl _contentControl;
Type _currentViewModelType;
IMvxViewModel _rootViewModel;
public ViewPresenter(ContentControl c) : base(c)
{
_contentControl = c;
AddPresentationHintHandler<SetRootHint>(SetRootHintHandler);
AddPresentationHintHandler<PopToRootHint>(PopToRootHintHandler);
}
protected override void ShowContentView(FrameworkElement element, MvxContentPresentationAttribute attribute, MvxViewModelRequest request)
{
base.ShowContentView(element, attribute, request);
_currentViewModelType = request.ViewModelType;
}
private bool SetRootHintHandler(SetRootHint hint)
{
_rootViewModel = hint.CurrentViewModel;
return true;
}
private bool PopToRootHintHandler(PopToRootHint hint)
{
// How to pop all the way down to _rootViewModel ?
return true;
}
}
How can I pop all the way back to _rootViewModel? Is there a better way of popping back multiple view models in one shot?
I ended up writing a helper class that keeps a reference of all the view models, as well as the one you set as the Root. And then I can just call my PopToRoot method.
public class NavigationStack
{
private readonly List<IMvxViewModel> _stack;
public IMvxViewModel Root { get; set; }
public NavigationStack()
{
_stack = new List<IMvxViewModel>();
}
public void AddToStack(IMvxViewModel viewModel)
{
_stack.Add(viewModel);
}
public async Task PopToRoot(IMvxNavigationService navigationService)
{
if (Root == null)
{
throw new Exception("Can not pop to root because Root is null.");
}
else
{
_stack.Reverse();
foreach (var v in _stack)
{
if (v != Root)
{
await navigationService.Close(v);
}
else
{
break;
}
}
_stack.Clear();
}
}
}
It works, but I'm not sure if this is a good idea since I'm keeping a reference to all the IMvxViewModel in my app, and closing them one after another...does anyone know if this code can cause any problems in the framework?

Exposing custom properties using UI Automation Framework

Given a very basic WinForms custom/user control, using System.Windows.Automation it is possible to manipulate built in properties for the custom control.
This is done like this:
public object GetPropertyValue(int propertyId)
{
if (propertyId == AutomationElementIdentifiers.NameProperty.Id)
{
return "Hello World!";
}
}
What I would like to do is expose custom properties to ui automation such as ReadyState, LastAccessed, Etc.
Is this possible?
No, you can't extend the list of properties, and this is complicated by the fact you use Winforms that has a poor UI Automation support (it uses IAccessible with bridges etc.).
What you can do though is add some fake objects to the automation tree, for example, here is a sample Winforms UserControl that does it:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Button button = new Button();
button.Location = new Point(32, 28);
button.Size = new Size(75, 23);
button.Text = "MyButton";
Controls.Add(button);
Label label = new Label();
label.Location = new Point(49, 80);
label.Size = new Size(35, 13);
label.Text = "MyLabel";
Controls.Add(label);
MyCustomProp = "MyCustomValue";
}
public string MyCustomProp { get; set; }
protected override AccessibleObject CreateAccessibilityInstance()
{
return new UserControl1AccessibleObject(this);
}
protected class UserControl1AccessibleObject : ControlAccessibleObject
{
public UserControl1AccessibleObject(UserControl1 ownerControl)
: base(ownerControl)
{
}
public new UserControl1 Owner
{
get
{
return (UserControl1)base.Owner;
}
}
public override int GetChildCount()
{
return 1;
}
public override AccessibleObject GetChild(int index)
{
if (index == 0)
return new ValueAccessibleObject("MyCustomProp", Owner.MyCustomProp);
return base.GetChild(index);
}
}
}
public class ValueAccessibleObject : AccessibleObject
{
private string _name;
private string _value;
public ValueAccessibleObject(string name, string value)
{
_name = name;
_value = value;
}
public override AccessibleRole Role
{
get
{
return AccessibleRole.Text; // activate Value pattern
}
}
// note you need to override with member values, base value cannot always store something
public override string Value { get { return _value; } set { _value = value; } }
public override string Name { get { return _name; } }
}
And this is how it appears in the automation tree (using the inspect.exe tool):
Note this technique also supports writing back to the property because it's based on the ValuePattern.

How to get FixedDocument from PageDocuments [duplicate]

This question already has answers here:
How to Print Preview when using a DocumentPaginator to print?
(6 answers)
Closed 4 years ago.
I am working with printing in WPF.
I implement a class which inherit from DocumentPaginator class
public class ReportPaginator : DocumentPaginator
{
private double pageUpperLimit;
private double pageDownLimit;
private Model.Report printedReport;
private int pageCount;
public override IDocumentPaginatorSource Source
{
get
{
return null;
}
}
public override bool IsPageCountValid
{
get { return true; }
}
public override int PageCount
{
get { return pageCount; }
}
public override Size PageSize
{
get
{
return new Size(printedReport.PageSize.Width, printedReport.PageSize.Height);
}
set
{
// validate the value.
if (value != null)
{
throw new ArgumentException("page size can not be null");
}
// TODO: here we have to validate if the page is A3, A4, A5. this is to set the SizeName.
// if you did not set the sizeName here we will get an exception.
printedReport.PageSize = new Model.PageSize { Height = value.Height, Width = value.Width };
CalculatesPage();
// if we have to set the PageSize (I do not think so), do not forget to call the PaginateData method.
}
}
public ReportPaginator( Model.Report report)
{
printedReport = report;
CalculatesPage();
}
public override DocumentPage GetPage(int pageNumber)
{
// validate the argument.
if (pageNumber < 0)
{
throw new ArgumentException("pageNumber parameter could not be negative number", "pageNumber");
}
// if the argument is outside of the available pages, return
if (pageNumber > pageCount - 1)
{
return DocumentPage.Missing;
}
// specify the start pixel and end pixel of the page according to the height of the report.
pageUpperLimit = pageNumber * PageSize.Height;
pageDownLimit = (pageNumber + 1) * PageSize.Height;
// create DrawingVisual for this DocumentPage
DrawingVisual visual = new DrawingVisual();
// get the DrawingContext for this DrawingVisual for this page.
using (DrawingContext pageContext = visual.RenderOpen())
{
// drawing operations for elements and sections go here.
// we will use the for loop instead of the foreach loop to enumerate the sections
// because we will need the know in which section we draw.
for (int sectionIndex = 0; sectionIndex < printedReport.Sections.Count; sectionIndex++)
{
PrintSection(pageContext, sectionIndex);
foreach (Model.BaseReportControl control in printedReport.Sections[sectionIndex].Controls)
{
PrintControl(pageContext, sectionIndex, control);
}
}
} // close the DrawingContext.
return new DocumentPage(visual);
}
}
there is more helper methods to do the jobs.
I want to create FixedDocument object to return it from the Source property
using DocumentPage object that returned from GetPage Method.
I need a FixedDocument to be returned from the source Property
Because I want to make a preview to the File before printing
ReportPreviewerWindow window = new ReportPreviewerWindow();
window.previewer.Document = new ReportPaginator(printedSurface.ModelReport).Source;
window.ShowDialog();
the previewer is DocumentViewer Object exists in the ReportPreviewerWindow
<Window x:Class="TanmiaGrp.Report.Designer.ReportPreviewerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Report Previewer" >
<Grid>
<DocumentViewer x:Name="previewer"/>
</Grid>
</Window>
After I asked this question I found something worked very correctly for me.
if you have problem like the described above please refer this link
How to Print Preview when using a DocumentPaginator to print?

Remove duplicates from listbox/combobox in WPF

I've seen answers mentioning something compact like this:here
List<T> withDupes = LoadSomeData();
List<T> noDupes = withDupes.Distinct().ToList();
So I tried the following (syntax)
List<InfoControl> withDupes = (List<InfoControl>)listBox1.ItemsSource;
listBox1.ItemsSource = withDupes.Distinct().ToList();
but withDupes is null ? Perhaps I am retrieving the wrong data list. I added InfoControls one at a time.
Is there something else I should be implementing in InfoControl class? (Equal,hashCode)?
Thanks
Addendum 1: [ignore I should not be translating from Java :) ]
Also have (translated from a Java example, not sure it's 100% correct) declared in the InfoControl class..
public Boolean Equals(Object obj)
{ if (obj == this) { return true; }
if (!(obj is InfoControl)) { return false; }
InfoControl other = (InfoControl)obj;
return this.URL.Equals(other.URL); }
public int hashCode()
{ return this.URLFld.Content.GetHashCode(); }
Addendum 2:
When I try to use override based on the msdn link custom type example it says it is sealed :)
It does not seem distinct is stepping thru GetHashCode() and I am still getting the same listbox.items.count after distinct.
bool IEquatable<InfoControl>.Equals(InfoControl other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(this, other)) return true;
return URL.Equals(other.URL);
}
public int GetHashCode(InfoControl obj)
{
return obj.URL.GetHashCode();
}
Addendum 3:
When I try override VS2010 says it is sealed? "cannot override inherited member 'System.Windows.DependencyObject.GetHashCode()' because it is sealed" what am I doing wrong?
public override int GetHashCode()
{
return URL.GetHashCode();
}
public string URL
{
get { return this.URLFld.Content.ToString() ; }
set
{
this.URLFld.Content = value;
}
}
.
Addendum 4:
public partial class InfoControl : UserControl
, IEquatable<YouTubeInfoControl>
{
private string URL_;
public string URL
{
get { return URL_; }
set
{
URL_ = value;
}
}
bool IEquatable<YouTubeInfoControl>.Equals(YouTubeInfoControl other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(this, other)) return true;
return URL == other.URL;
}
public override int GetHashCode()
{
return URL.GetHashCode();
}
}
A ListBox's items can either be set via ListBox.Items or ListBox.ItemsSource, if you add items using listBox1.Items.Add this does not affect the ItemsSource which will stay null. In this case you should get your initial list from listBox1.Items.
If you're adding the InfoControl objects one at a time, the ItemSource of the listBox will remain set to NULL. You're better off binding a List to the listbox which will allow you to get the data back off of the ItemSource property later

CommandBinding question. How to enable command button

My code is here>>
public class Player:INotifyPropertyChanging
{
string addressBar;
public string Url
{
get {
return addressBar;
}
set { addressBar = value; OnPropertyChanged("Url"); }
}
public Regex regVillage = new Regex(#"\?doc=\d+&sys=[a-zA-Z0-9]{2}");
RelayCommand _AddAttackTask;
public ICommand AddAttackTask
{
get {
if (_AddAttackTask == null)
{
_AddAttackTask = new RelayCommand(param =>
{
}, param => this.CanAttack);
}
return _AddAttackTask;
}
}
public Boolean CanAttack
{
get{
if (Url == null) return false;
return regVillage.IsMatch(Url);
}
}
}
On the xaml, i have textbox and button. Textbox binded by url, button binded by AddAttackTask. When i change textbox value,Url changed.Main target is When changing url, button bring to enable or disable. But button always disabled.
I'm getting RelayCommand class from WPF Apps With The Model-View-ViewModel Design Pattern
What is wrong on my code?
Please fix my command binding!
I found it yourself.
Must call CommandManager.InvalidateRequerySuggested(); function after changing property
public string Url
{
get {
return addressBar;
}
set { addressBar = value; OnPropertyChanged("Url");
CommandManager.InvalidateRequerySuggested();
}
}

Resources