How to open Color and Font Dialog box using WPF? - wpf

I want to show the color and font dialog box in WPF .net 4.5, how to can I do?
Please help me anybody.
Thnx in Advanced!

The best out of the box solution is using FontDialog form System.Windows.Forms assembly, but you will have to convert it's output to apply it to WPF elements.
FontDialog fd = new FontDialog();
var result = fd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
Debug.WriteLine(fd.Font);
tbFonttest.FontFamily = new FontFamily(fd.Font.Name);
tbFonttest.FontSize = fd.Font.Size * 96.0 / 72.0;
tbFonttest.FontWeight = fd.Font.Bold ? FontWeights.Bold : FontWeights.Regular;
tbFonttest.FontStyle = fd.Font.Italic ? FontStyles.Italic : FontStyles.Normal;
TextDecorationCollection tdc = new TextDecorationCollection();
if (fd.Font.Underline) tdc.Add(TextDecorations.Underline);
if (fd.Font.Strikeout) tdc.Add(TextDecorations.Strikethrough);
tbFonttest.TextDecorations = tdc;
}
Notice that winforms dialog does not support many of WPF font properties like extra bold fonts.
Color dialog is much easier:
ColorDialog cd = new ColorDialog();
var result = cd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
tbFonttest.Foreground = new SolidColorBrush(Color.FromArgb(cd.Color.A, cd.Color.R, cd.Color.G, cd.Color.B));
}
It does not support alpha though.

You can use classes from System.Windows.Forms, there is nothing wrong with using them. You'll probably need to convert values to WPF-specific though.
Alternatively, you can implement your own dialogs or use third-party controls, see Free font and color chooser for WPF?.

Related

Not able to enter text in a edit field inside a modal/dialog using Teststack/White

I am trying to enter text in a edit field in a modal window. I get an error "Failed to get (ControlType=edit or ControlType=document),AutomationId=1118,ClassName=Edit"
The following is my code.
var window = app.GetWindow("Toolkit Version");
Window AuthWindow = null;
AuthWindow = window.ModalWindow("Please Authenticate");
TextBox userNameField = AuthWindow.Get<TextBox>(SearchCriteria.ByClassName("Edit").AndAutomationId("1118"));
userNameField.Text = "Administrator";
From Inspect
Error details -
TestStack.White.AutomationException: 'Failed to get (ControlType=edit or ControlType=document),AutomationId=1118,ClassName=Edit'
Any suggestions or workarounds?
Thanks!
Off the top of my head:
Perhaps your SearchCriteria are too restrictive? Try:
TextBox userNameField = AuthWindow.Get<TextBox>(SearchCriteria.ByAutomationId("1118"));
or even
TextBox userNameField = AuthWindow.Get(SearchCriteria.ByAutomationId("1118")) as TextBox;
It might not be very elegant, but it looks like your window is small and has few controls. Why not picking them this way?
TextBox userNameField = AuthWindow.GetMultiple(SearchCriteria.ByControlType(System.Windows.Automation.ControlType.Edit)[0]
I supposed your textbox is at position 0 but of course you can change that.

codenameone Picker Alternative to ComboBox

I am getting my feet wet with Codename One. I have looked into more other options like Xamarin, PhoneGap, Ionic for cross platform but I kinda got hooked with Codename one as it really code once and run anywhere.
I've been going through ui elements and I am kinda blocked on populating a combobox (Alternative is Picker)
Let's say I have stores as value pair (storeId, storeName). I want to display the storeName in Picker but keep storeId as the value reference.
Once the store is selected I would like to pass the storeId to an API call.
Is this possible. This might be very simple question but seems bit difficult to implement (I am really new to mobile).
Thank you.
Our recommendation is to avoid ComboBox. It's a UI pattern that doesn't exist on iOS natively and would feel alien on modern phones. It exists in Codename One.
In this code from the sample above you can get a similar effect to a complex multi-field combo box:
Form hi = new Form("Button", BoxLayout.y());
String[] characters = { "Tyrion Lannister", "Jaime Lannister", "Cersei Lannister"};
String[] actors = { "Peter Dinklage", "Nikolaj Coster-Waldau", "Lena Headey"};
int size = Display.getInstance().convertToPixels(7);
EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(size, size, 0xffcccccc), true);
Image[] pictures = {
URLImage.createToStorage(placeholder, "tyrion","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/tyrion-lannister-512x512.jpg"),
URLImage.createToStorage(placeholder, "jaime","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/jamie-lannister-512x512.jpg"),
URLImage.createToStorage(placeholder, "cersei","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/cersei-lannister-512x512.jpg")
};
MultiButton b = new MultiButton("Pick A Lanister...");
b.addActionListener(e -> {
Dialog d = new Dialog();
d.setLayout(BoxLayout.y());
d.getContentPane().setScrollableY(true);
for(int iter = 0 ; iter < characters.length ; iter++) {
MultiButton mb = new MultiButton(characters[iter]);
mb.setTextLine2(actors[iter]);
mb.setIcon(pictures[iter]);
d.add(mb);
mb.addActionListener(ee -> {
b.setTextLine1(mb.getTextLine1());
b.setTextLine2(mb.getTextLine2());
b.setIcon(mb.getIcon());
d.dispose();
b.revalidate();
});
}
d.showPopupDialog(b);
});
hi.add(b);
hi.show();
If you insist on using a ComboBox you can use a model to give it any object data you want. Then create a cell render to display the data. This is all discussed in depth in the component section of Codname One's developer guide. Notice that since ComboBox derives from List a lot of the List tips and docs apply to ComboBox.

Codenameone: Alert Dialog message

we want dialog message in this format and look and fill
Can you please let me know how to resolve it. My application needs to be supported on all platforms (Android, iOS, Windows) and I don't want to write native code for all platforms separately.
Actually customizing the look is easier in Codename One as everything is written in Java you can customize literally everything about the look of anything.
For simplicity sake I used code rather than styles which would be better, you can customize the Dialog UIID and other UIID's in the theme designer to get more flexibility and have this easier. However, this would require many screenshots and explanations so I did the customization in code:
Form f = new Form("Test");
Button b = new Button("Show Dialog");
f.add(b);
b.addActionListener(e -> {
Dialog dlg = new Dialog("Authentication");
Style dlgStyle = dlg.getDialogStyle();
dlgStyle.setBorder(Border.createEmpty());
dlgStyle.setBgTransparency(255);
dlgStyle.setBgColor(0xffffff);
Label title = dlg.getTitleComponent();
title.setIcon(finalDuke.scaledHeight(title.getPreferredH()));
title.getUnselectedStyle().setFgColor(0xff);
title.getUnselectedStyle().setAlignment(Component.LEFT);
dlg.setLayout(BoxLayout.y());
Label blueLabel = new Label();
blueLabel.setShowEvenIfBlank(true);
blueLabel.getUnselectedStyle().setBgColor(0xff);
blueLabel.getUnselectedStyle().setPadding(1, 1, 1, 1);
blueLabel.getUnselectedStyle().setPaddingUnit(Style.UNIT_TYPE_PIXELS);
dlg.add(blueLabel);
TextArea ta = new TextArea("This is the text you want to appear in the dialog, this might line break if the text is too long...");
ta.setEditable(false);
ta.setUIID("DialogBody");
ta.getAllStyles().setFgColor(0);
dlg.add(ta);
Label grayLabel = new Label();
grayLabel.setShowEvenIfBlank(true);
grayLabel.getUnselectedStyle().setBgColor(0xcccccc);
grayLabel.getUnselectedStyle().setPadding(1, 1, 1, 1);
grayLabel.getUnselectedStyle().setPaddingUnit(Style.UNIT_TYPE_PIXELS);
dlg.add(grayLabel);
Button ok = new Button(new Command("OK"));
ok.getAllStyles().setBorder(Border.createEmpty());
ok.getAllStyles().setFgColor(0);
dlg.add(ok);
dlg.showDialog();
});
f.show();
I would recommend doing the dialog customization in the theme designer and using a 9-piece image border which is better looking.

Unable to draw polygon in gmap.net wpf

I am using gmap.net for my wpf application but i am unable to draw polygon in wpf. I found lot of tutorials of windows form for polygon and all those are working fine. But I didn't find any solution of wpf please help me!
You can use this code for make polygon
//Declare List for pointlatlang
List<PointLatLng> pointlatlang = new List<PointLatLng>();
pointlatlang.Add(new PointLatLng(-6.9143433, 107.6014166));
pointlatlang.Add(new PointLatLng(-6.9143416, 107.6013700));
//Declare polygon in gmap
GMapPolygon polygon = new GMapPolygon(pointlatlang);
mymap.RegenerateShape(polygon);
//setting line style
(polygon.Shape as Path).Stroke = Brushes.DarkBlue;
(polygon.Shape as Path).StrokeThickness = 1.5;
(polygon.Shape as Path).Effect = null;
//To add polygon in gmap
mymap.Markers.Add(polygon);
I believe this may have been a typo, but in line 7:
polygon.RegenerateShape(mymap);
You should have instead:
mymap.RegenerateShape(polygon);
//Declare List for pointlatlang
List<PointLatLng> pointlatlang = new List<PointLatLng>();
pointlatlang.Add(new PointLatLng(-6.9143433, 107.6014166));
pointlatlang.Add(new PointLatLng(-6.9143416, 107.6013700));
//Declare polygon in gmap
GMapPolygon polygon = new GMapPolygon(pointlatlang);
Path path = new Path();
path.Fill = new SolidColorBrush(Colors.Red) { Opacity=0.5};
path.StrokeThickness = 1.5;
path.Stroke = Brushes.DarkBlue;
path.Effect = null;
polygon.Shape = path;
//To add polygon in gmap
mymap.Markers.Add(polygon);
although i didn't find any solution of my problem but i found another alternative way to solve it. I have integrated windows form control of Gmap.net in WPF applications and used gmap.net dll for windows form.

Best way to show huge text in WPF?

I need to show a really huge amount of text data in WPF code. First i tried to use TextBox (and of course it was too slow in rendering). Now i'm using FlowDocument--and its awesome--but recently i have had another request: text shouldnt be hyphenated. Supposedly it is not (document.IsHyphenationEnabled = false) but i still don't see my precious horizontal scroll bar. if i magnify scale text is ... hyphenated.
public string TextToShow
{
set
{
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(value);
FlowDocument document = new FlowDocument(paragraph);
document.IsHyphenationEnabled = false;
flowReader.Document = document;
flowReader.IsScrollViewEnabled = true;
flowReader.ViewingMode = FlowDocumentReaderViewingMode.Scroll;
flowReader.IsPrintEnabled = true;
flowReader.IsPageViewEnabled = false;
flowReader.IsTwoPageViewEnabled = false;
}
}
That's how i create FlowDocument - and here comes part of my WPF control:
<FlowDocumentReader Name="flowReader" Margin="2 2 2 2" Grid.Row="0" />
Nothing criminal =))
I'd like to know how to tame this beast - googled nothing helpful. Or you have some alternative way to show megabytes of text, or textbox have some virtualization features which i need just to enable. Anyway i'll be happy to hear your response!
It's really wrapping not hyphenation. And one can overcome this by setting FlowDocument.PageWidth to reasonable value, the only question was how to determine this value.
Omer suggested this recipe msdn.itags.org/visual-studio/36912/ but i dont like using TextBlock as an measuring instrument for text. Much better way:
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(value);
FormattedText text = new FormattedText(value, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(paragraph.FontFamily, paragraph.FontStyle, paragraph.FontWeight, paragraph.FontStretch), paragraph.FontSize, Brushes.Black );
FlowDocument document = new FlowDocument(paragraph);
document.PageWidth = text.Width*1.5;
document.IsHyphenationEnabled = false;
Omer - thanks for the direction.

Resources