How to set DefaultFont to custom font in OxyPlot? - c#

I am using OxyPlot in a WPF application to plot a simple LineSeries. I also defined Roboto as the standard font of my application. However my PlotView does not use this font but uses its default font Segoe UI.
The PlotModel offers a DefaultFont property to change this font. This property does not accept a FontFamily but only a string. So if I try to modify the font by entering a FontFamily name which is installed on my system it works:
var model = new PlotModel
{
DefaultFont = "Verdana"
};
If I try to do the same with Roboto it does not:
var model = new PlotModel
{
DefaultFont = "./Resources/Roboto/#Roboto"
};
or
var model = new PlotModel
{
DefaultFont = "/MyNamespace;component/Resources/Roboto/#Roboto"
};
What am I missing here? Is there maybe another way to accomplish this?
Note: This is my first question, please tell me what I can improve in the future.

Related

How to change separator color in my IDesigner?

I just created my own library with designer control,that used from System.ComponentModel.Design reference.So i got a winform designer in my application,like in vs.But i dont like that separator white color,how i can change it,because i dont found the property of separator color.I know that is possible to change the separator color like to grey color,because visual studio separator is grey...
Image: First image is visual studio separator in designer,
Second image is ugly white separator in my IDesignerHost designer..
i we tried to find some property of separator color in IDesigner and IDesignerHost,but i dont found..please help me..
How my library with IDesignerHost is works:
//surface is interface of my implemented designer,so i can get it as control
//to change basic properties,like backcolor,font,etc...
//but also i can get it as IDesigner and IDesignerHost
//but how to change a separator color?
DesignSurface surface = new DesignSurface();
Control view = surface.GetView(); //returns control or IDesigner/IDesignerHost
view.BackColor = Color.FromArgb(30, 30, 30);
You can get the splitter from Controls collection of the view of the DesignSurface. The splitter will be added as soon as you have a component in the component tray. So you can use this code to change color of the splitter.
Example
You can try it using the following code. Just make sure you have added a reference to System.Design assembly and using System.Linq and System.ComponentModel.Design namespaces:
var surface = new DesignSurface();
var host = (IDesignerHost)surface.GetService(typeof(IDesignerHost));
surface.BeginLoad(typeof(Form));
var root = (Form)host.RootComponent;
host.CreateComponent(typeof(BindingSource), "bindingSource1");
var view = (Control)surface.View;
view.Dock = DockStyle.Fill;
view.BackColor = Color.White;
var splitter = view.Controls.OfType<Splitter>().FirstOrDefault();
if (splitter != null)
splitter.BackColor = Color.Red;
else
{
view.ControlAdded += (obj, args) =>
{
if (args.Control is Splitter)
args.Control.BackColor = Color.Red;
};
}
this.Controls.Add(view);
And you will get a result like:

Livecharts: Zoom to a specific region in CartesianChart

I am using LiveCharts.WinForms.CartesianChart and load it with a GLineSeries. What I want to do, is to zoom to specific region of the X-Axis. Is it somehow possible in LiveCharts?
I could not find any method CartesianChart doing that.
I have found a way to do that. I think the MinValue and MaxValue mentioned in the comments will perhaps work, too. But instead of changing the MinValue and MaxValue directly, I found another way. You can bind the CartesianChart to a BindingAssistant. There you can set the From and To Value. Then the CartesianChart is automatically zoomed to the region.
The code would look like this:
var assistant = new BindingAssistant
{
From = ZoomDateStart.Ticks,
To = ZoomDateEnd.Ticks
};
cartesianChart1.AxisX[0].SetBinding(Axis.MinValueProperty,
new Binding { Path = new PropertyPath("From"), Source = assistant, Mode = BindingMode.TwoWay });
cartesianChart1.AxisX[0].SetBinding(Axis.MaxValueProperty,
new Binding { Path = new PropertyPath("To"), Source = assistant, Mode = BindingMode.TwoWay });
The advantage of using this method is that now the chart can also be made scrollable. For more info: Example

Use Custom Font in DrawText Wpf C#

I have a custom OTF font in the Resources folder in my project which is not installed on my Windows.
The Build Action of the font is already set to Resource.
Now I want to use DrawText method and FormattedText class to write some text on visual layer.
How can I use this custom font for the FormattedText. I already know how to do this say for a TextBlock in XAML using the below code. But what about code-behind?
<TextBlock FontFamily="pack://application:,,,/Resources/#Quicksand Light">
StackOverflow
</TextBlock>
Here is the code I'm using to define my FormattedText object.
var f = new FontFamily("pack://application:,,,/Resources/#Quicksand Light");
var typeface = new Typeface(f, new FontStyle(), new FontWeight(), new FontStretch());
var cultureinfo = new CultureInfo("en-us");
var ft = new FormattedText("Stackoverflow", cultureinfo, FlowDirection.LeftToRight,
typeface, 28, Brushes.White)
dc.DrawText(ft, new Point(0,0));
My problem is is defining the font for typeface so that I can use it in fotmattedtext.
Something like this:
var typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,/"), "/Resources/#Quicksand Light"), FontStyles.Normal, FontWeights.Regular, FontStretches.Normal);

How to check if a font supports a specific style

I'm getting the following exception when changing my application font, because I use a strike out in a part of my application, and some fonts don't support it:
I change my application font using a font dialog. I need to check if the selected font supports the strikeout style after assigning it to my application.
What is the recommended way to do this? I know I could create a font with the style and catch the exception, but is there a more elegant way to do it?
Thanks in advance.
EDIT: The user selects a font, not necesary strikeout. In that moment I need to check if the font supports the style strikeout, because I create a strikeout font in a part of my application. If the font don't support the strikeout style would not allow the user to choose that font.
Updated : (to reflect update in the initial post):
InstalledFontCollection ifc = new InstalledFontCollection();
for (int i = 0; i < ifc.Families.Length; i++)
{
if (ifc.Families[i].IsStyleAvailable(FontStyle.StrikeOut))
{
//add particular font with this family to your "font selector"
}
}
If you are using the standard Font class, then you can use the Font.Strikeout property:
//Gets a value that indicates whether this Font specifies a horizontal line through the font.
public bool Strikeout { get; }
Finally I used the following:
private bool SupportStrikeout(Font font)
{
try
{
using (Font strikeout = new Font(font, FontStyle.Strikeout))
{
return true;
}
}
catch (ArgumentException)
{
return false;
}
}

How-To set Height of a Textbox?

For my single line Textbox, I set is Border = None. On doing this, the height turns very small. I can't programamtically set the height of the textbox. If I set any border, then again its fine, but I don't want any border. Even the text is not visible completely - so the font size is already bigger the the textbox height.
I tried creating a custom textbox, and set the Height of it, but it has no effect. How to handle this situation? Any help is highly appreciated.
There is a simple way not to create a new class.
In Designer.cs file:
this.textBox1.AutoSize = false;
this.textBox1.Size = new System.Drawing.Size(228, 25);
And that's all.
TextBox derives from Control, which has an AutoSize property, but the designers have hidden the property from the PropertyGrid and Intellisense, but you can still access it:
public class TextBoxWithHeight : TextBox {
public TextBoxWithHeight() {
base.AutoSize = false;
}
}
Rebuild and use.
TextBox controls automatically resize to fit the height of their Font, regardless of the BorderStyle you choose. That's part of the defaults used by Visual Studio.
By changing the Multiline, you can override the Height.
this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif",
26.25F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.textBox1.Location = new System.Drawing.Point(373, 502);
// this is what makes the height 'stick'
this.textBox1.Multiline = true;
// the desired height
this.textBox1.Size = new System.Drawing.Size(100, 60);
Hope this helps.
I just created this case in an empty project and don't see the result you are describing.
When the BorderStyle is none, the display area of the Textbox auto-sizes to the font selected. If I then set Multiline = true, I can change the height portion of the Size property and the change sticks.
Perhaps another portion of your code is modifying the height? A resize event handler perhaps?
My suggestions:
Post the relevant portions of your code
Try to reproduce the issue in an empty WinForms project (as I just did)
I find the best solution is to subclass the Textbox and expose the hidden AutoSize there:
public class TextBoxWithHeight : TextBox
{
public bool Auto_Size
{
get { return this.AutoSize; }
set { this.AutoSize = value; }
}
}
Now you can set the Autosize on or off using the object inspector in the visual designer or in code, whatever you prefer.
Just select your textbox and go to properties then increase your font size.. DONE !!!

Categories