GMAP.NET, adding labels to markers - winforms

I am using GMAPS in C# (Winforms) and I would like to add a marker with a label. I followed the answer at GMAP.NET adding labels underneath markers and noticed that there is an issue with the implementation. The markers are not plotted in the correct place and the labels are all plotted on top of each other. I think it is not correctly calling the OnRender method for the marker? Can anyone point me in the right direction?

In ran into the same issue and just calling base.OnRender(g); wasn't fixing it for me. The trick is to derive from GMarkerGoogle instead of GMapMarker as done in the answer you provided.
Also I had to do some tweaks with the text rendering. I came up with this solutions, works fine for me:
public class GmapMarkerWithLabel : GMarkerGoogle, ISerializable
{
private readonly Font _font;
private GMarkerGoogle _innerMarker;
private readonly string _caption;
public GmapMarkerWithLabel(PointLatLng p, string caption, GMarkerGoogleType type)
: base(p, type)
{
_font = new Font("Arial", 11);
_innerMarker = new GMarkerGoogle(p, type);
_caption = caption;
}
public override void OnRender(Graphics g)
{
base.OnRender(g);
var stringSize = g.MeasureString(_caption, _font);
var localPoint = new PointF(LocalPosition.X - stringSize.Width / 2, LocalPosition.Y + stringSize.Height);
g.DrawString(_caption, _font, Brushes.Black, localPoint);
}
public override void Dispose()
{
if (_innerMarker != null)
{
_innerMarker.Dispose();
_innerMarker = null;
}
base.Dispose();
}
#region ISerializable Members
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
GetObjectData(info, context);
}
protected GmapMarkerWithLabel(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
#endregion
}

Related

RibbonApplicationMenu: getting rid of the AuxiliaryPane

It so happened that the application I'm working on doesn't operate on documents, so there's no need in displaying the recently opened documents list in the application menu.
But - annoyingly - there are no properties readily available in the RibbonApplicationMenu class to hide the unused AuxiliaryPane (for which, curiously, the property does exist, but is marked as "internal").
Of course, I can just leave it there - but that's... untidy.
So, here's the solution I came up with.
Hope it will be helpful for anyone else :-)
The general idea is to subclass the RibbonApplicationMenu, find the template child corresponding to the menu's Popup, and overrule its Width (after a number of frustrating experiments it became evident that doing that neither for PART_AuxiliaryPaneContentPresenter nor for PART_FooterPaneContentPresenter - nor for the both - could achieve anything).
Well, without further ado, here's the code:
public class SlimRibbonApplicationMenu : RibbonApplicationMenu
{
private const double DefaultPopupWidth = 180;
public double PopupWidth
{
get { return (double)GetValue(PopupWidthProperty); }
set { SetValue(PopupWidthProperty, value); }
}
public static readonly DependencyProperty PopupWidthProperty =
DependencyProperty.Register("PopupWidth", typeof(double),
typeof(SlimRibbonApplicationMenu), new UIPropertyMetadata(DefaultPopupWidth));
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.DropDownOpened +=
new System.EventHandler(SlimRibbonApplicationMenu_DropDownOpened);
}
void SlimRibbonApplicationMenu_DropDownOpened(object sender, System.EventArgs e)
{
DependencyObject popupObj = base.GetTemplateChild("PART_Popup");
Popup popupPanel = (Popup)popupObj;
popupPanel.Width = (double)GetValue(PopupWidthProperty);
}
}
As a side note, I tried to find any way to resolve the desired width based on the max width of the ApplicationMenu's Items (rather than setting it explicitly through the DependencyProperty in XAML) - but to no avail.
Given my despise to "magic numbers", any suggestion on that will be deeply appreciated.
I know this has been a while, but I've got another solution to this. This one does not provide the Popup width property, instead a ShowAuxilaryPanel boolean. It then goes to Bind the width of the Popup, to the width of the menu item area of the menu.
public class SlimRibbonApplicationMenu : RibbonApplicationMenu
{
public bool ShowAuxilaryPanel
{
get { return (bool)GetValue(ShowAuxilaryPanelProperty); }
set { SetValue(ShowAuxilaryPanelProperty, value); }
}
public static readonly DependencyProperty ShowAuxilaryPanelProperty =
DependencyProperty.Register("ShowAuxilaryPanel", typeof(bool),
typeof(SlimRibbonApplicationMenu), new UIPropertyMetadata(true));
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.DropDownOpened += SlimRibbonApplicationMenu_DropDownOpened;
}
void SlimRibbonApplicationMenu_DropDownOpened(object sender, EventArgs e)
{
DependencyObject popupObj = base.GetTemplateChild("PART_Popup");
Popup panel = (Popup)popupObj;
var exp = panel.GetBindingExpression(Popup.WidthProperty);
if (!this.ShowAuxilaryPanel && exp == null)
{
DependencyObject panelArea = base.GetTemplateChild("PART_SubMenuScrollViewer");
var panelBinding = new Binding("ActualWidth")
{
Source = panelArea,
Mode = BindingMode.OneWay
};
panel.SetBinding(Popup.WidthProperty, panelBinding);
}
else if (this.ShowAuxilaryPanel && exp != null)
{
BindingOperations.ClearBinding(panel, Popup.WidthProperty);
}
}
}
worked for me
<telerik:ApplicationMenu RightPaneVisibility="Collapsed" >

LINQ binding refresh problem

I have a ListView bound to a LINQ to SQL object. When I double click an article in the ListView it opens the article details window and allow the user to change article properties.
So far, it all works fine, but when the user saves and closes the article details, the ListView doesn't reflect the changes made (like the article's description for example). I don't want to implement INotifyPropertyChanged in all my LINQ classes because I use VS2010 to generate my Linq table schema, so it would be a pain to alter auto generated designer code... (and it will certainly override all my changes each time I will make a change to the table's schema)
How can I simply force the ListView to refresh the LINQ binding when the details window is closed?
Thank in advance for your help.
All Linq classes are generated as partial classes - this means you can create your own partial class that matches the Linq class and add any extra functionality required there. Then when it is compiled, it will all work as one class.
A quick and easy solution is to use a DynamicObject decorator to add the change notificcation behaviour without having to change your original classes, or writing a suite of partial class definitions
public class DynamicBindingProxy<T> : DynamicObject, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private static readonly Dictionary<string, Dictionary<string,
PropertyInfo>> properties = new Dictionary<string,
Dictionary<string, PropertyInfo>>();
private readonly T instance;
private readonly string typeName;
public DynamicBindingProxy(T instance)
{
this.instance = instance;
var type = typeof(T);
typeName = type.FullName;
if (!properties.ContainsKey(typeName))
SetProperties(type, typeName);
}
private static void SetProperties(Type type, string typeName)
{
var props = type.GetProperties(
BindingFlags.Public | BindingFlags.Instance);
var dict = props.ToDictionary(prop => prop.Name);
properties.Add(typeName, dict);
}
public override bool TryGetMember(GetMemberBinder binder,
out object result)
{
if (properties[typeName].ContainsKey(binder.Name))
{
result = properties[typeName][binder.Name]
.GetValue(instance, null);
return true;
}
result = null;
return false;
}
public override bool TrySetMember(SetMemberBinder binder,
object value)
{
if (properties[typeName].ContainsKey(binder.Name))
{
properties[typeName][binder.Name]
.SetValue(instance, value, null);
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(binder.Name));
return true;
}
return false;
}
}
and heres a sample useage:
public partial class MainWindow : Window
{
private readonly TestObj tObj;
private DynamicBindingProxy<TestObj> dynObj;
public MainWindow()
{
InitializeComponent();
tObj = new TestObj() { Name = "test", Amount = 123.45, ID = 44, SomeDate = DateTime.Now };
dynObj = new DynamicBindingProxy<TestObj>(tObj);
DataContext = dynObj;
}
private void UpdateName(object sender, RoutedEventArgs e)
{
((dynamic)dynObj).Name = newText.Text;
}
}
full details can be found on a blog post I wrote specifically about this issues
http://www.deanchalk.me.uk/post/WPF-e28093-Easy-INotifyPropertyChanged-Via-DynamicObject-Proxy.aspx

Highlight Search TextBlock

My goal is to create a custom TextBlock control that has a new dependency property, SearchText. This property will contain a regular expression. All occurrences of this regular expression in the text of the TextBlock will be highlighted using a custom style (another DP).
My current implementation involves clearing all of the Inline objects in the TextBlock's InlineCollection. I then fill the TextBlock with runs for unhighlighted text and runs for highlighted text with the style applied (this method does not support adding inlines directly to the TextBlock, instead TextBlock.TextProperty has to be used).
Works great, but sometimes I get a strange exception when trying to clear the Inlines: InvalidOperationException: "Cannot modify the logical children for this node at this time because a tree walk is in progress."
This problem seems to be related to this one. I am modifying the inlines in the TextChanged function, but I'm using a flag to avoid infinite recursive edits.
Any thoughts on how to architect this custom control? Is there a better way to do this? How do I get around this exception?
Thanks!
In my implementation, I solved this by just adding another dependency property, called OriginalText. When it's modified, I updated both the Text property and update the highlighting. Here's the code:
public class HighlightTextBlock : TextBlock
{
public string HighlightedText
{
get { return (string)GetValue(HighlightedTextProperty); }
set { SetValue(HighlightedTextProperty, value); }
}
public static readonly DependencyProperty HighlightedTextProperty =
DependencyProperty.Register("HighlightedText", typeof(string), typeof(HighlightTextBlock), new UIPropertyMetadata(string.Empty, UpdateHighlightEffect));
public static readonly DependencyProperty OriginalTextProperty = DependencyProperty.Register(
"OriginalText", typeof(string), typeof(HighlightTextBlock), new PropertyMetadata(default(string), OnOriginalTextChanged));
private static void OnOriginalTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var block = ((HighlightTextBlock)obj);
block.Text = block.OriginalText;
block.UpdateHighlightEffect();
}
public string OriginalText
{
get { return (string)GetValue(OriginalTextProperty); }
set { SetValue(OriginalTextProperty, value); }
}
private static void UpdateHighlightEffect(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (!(string.IsNullOrEmpty(e.NewValue as string) && string.IsNullOrEmpty(e.OldValue as string)))
((HighlightTextBlock)sender).UpdateHighlightEffect();
}
private void UpdateHighlightEffect()
{
if (string.IsNullOrEmpty(HighlightedText)) return;
var allText = GetCompleteText();
Inlines.Clear();
var indexOfHighlightString = allText.IndexOf(HighlightedText, StringComparison.InvariantCultureIgnoreCase);
if (indexOfHighlightString < 0)
{
Inlines.Add(allText);
}
else
{
Inlines.Add(allText.Substring(0, indexOfHighlightString));
Inlines.Add(new Run()
{
Text = allText.Substring(indexOfHighlightString, HighlightedText.Length),
Background = Consts.SearchHighlightColor,
});
Inlines.Add(allText.Substring(indexOfHighlightString + HighlightedText.Length));
}
}
private string GetCompleteText()
{
var allText = Inlines.OfType<Run>().Aggregate(new StringBuilder(), (sb, run) => sb.Append(run.Text), sb => sb.ToString());
return allText;
}
}
Still not sure if there's a better way to do this altogether, but I appear to have found a work around.
I was updating the inlines/runs in a function that was fired by the change notification for the TextProperty and the SearchTextProperty.
Now I'm firing the highlight/update code from a Dispatcher.BeginInvoke() call in the change notification with DispatcherPriority.Normal.
In case anyone wants an example of how to do this, I found this

How to set form object property globally in .net winforms?

i am using .net win forms i need to set some common properties globally to my win forms like css in web application
ex
form background color=red
button width =100
Text box width=200
font family=arial
how to do this?
how about create a base form that all the other forms inherit.
On the base form you can set the common look and feel.
then if it is necessary to overwrite the common properties you can do so.
EDIT
something like this for the base form.
public partial class BaseForm : Form
{
private Font _font = new Font("Arial", 10);
private Color _backColor = Color.Red;
public BaseForm()
{
InitializeComponent();
}
public override Font Font
{
get { return _font; }
set { _font = value; }
}
public override Color BackColor
{
get { return _backColor; }
set { _backColor = value; }
}
}
and this for the form that you want to display
public partial class Form1 : BaseForm
{
public Form1()
{
InitializeComponent();
}
}
Use the App.Config file or the Settings tab in project properties.
You could create a static class to store them - maybe in a Dictionary perhaps.
Something like this could work:
public static class GlobalData
{
private static Dictionary<string, object> settings;
private static void SetDefaults()
{
settings = new Dictionary<string, object>();
settings.Add("BackgroundColour", "Red");
settings.Add("Width", 100);
}
public static Dictionary<string, object> FormSettings
{
get {
if (settings ==null)
{
SetDefaults();
}
return settings;
}
}
}
EDIT:
You could you use it like this:
this.Width = Convert.ToInt32(GlobalData.FormSettings["Width"].ToString());

WPF - Is there any way to programmatically evaluate a binding?

Does anyone know how to get the current value associated with a Binding? I ran into a problem recently where I wanted to get the value associated with a particular cell in the WPFToolKit DataGrid - so I created a function that gets the Path string, splits on '.' and tries uses PropertyDescriptor in a loop, trying to get the bound value. Surely there's a better way :). If anyone can point me in the right direction, I'll love you forever.
Thanks,
Charles
As the given link to the answer is nowadays only available on webarchive I duplicated the answer that was given there:
public static class DataBinder
{
private static readonly DependencyProperty DummyProperty = DependencyProperty.RegisterAttached(
"Dummy",
typeof(Object),
typeof(DependencyObject),
new UIPropertyMetadata(null));
public static object Eval(object container, string expression)
{
var binding = new Binding(expression) { Source = container };
return binding.Eval();
}
public static object Eval(this Binding binding, DependencyObject dependencyObject = null)
{
dependencyObject = dependencyObject ?? new DependencyObject();
BindingOperations.SetBinding(dependencyObject, DummyProperty, binding);
return dependencyObject.GetValue(DummyProperty);
}
}
Example:
public partial class PropertyPathParserDemo : Window
{
public PropertyPathParserDemo()
{
InitializeComponent();
Foo foo = new Foo() { Bar = new Bar() { Value = "Value" } };
this.Content = DataBinder.Eval(foo, "Bar.Value");
}
public class Foo
{
public Bar Bar
{
get;
set;
}
}
public class Bar
{
public string Value
{
get;
set;
}
}
}

Resources