It would be useful to attach a bunch of custom properties to standard controls in .NET, without creating a set of custom controls.
I heard that its possible with WPF, but does somebody knows a solution for plain winform application?
Why? For example, when a control has the focus and its a TextBox, or a DropDownList or a ComboBox, then I want to paint its backcolor in the custom property I could name: "focusColor"
And of course, I do not want to create custom buttons, combos or dropdown...
In WPF it is done with attached properties. Although there is no exact equivalent in Windows Form, you can implement an IExtenderProvider to "attach" a custom property to a control. This property can then be used in the designer, like a normal property. See the code provided in this blog post for an example.
Here's an implementation for your "FocusColor" example:
[ProvideProperty("FocusColor", typeof(Control))]
public class FocusColorProvider : Component, IExtenderProvider
{
private readonly Dictionary<IntPtr, Color> _focusColors;
private readonly Dictionary<IntPtr, Color> _backColors;
public FocusColorProvider()
{
_focusColors = new Dictionary<IntPtr, Color>();
_backColors = new Dictionary<IntPtr, Color>();
}
public bool CanExtend(object extendee)
{
return (extendee is Control);
}
public Color GetFocusColor(Control ctl)
{
Color color;
if (_focusColors.TryGetValue(ctl.Handle, out color))
{
return color;
}
return ctl.BackColor;
}
public void SetFocusColor(Control ctl, Color color)
{
Color backColor;
if (!_backColors.TryGetValue(ctl.Handle, out backColor))
{
backColor = ctl.BackColor;
}
// Same color as BackColor, disable the behavior
if (color == backColor)
{
RemoveFocusColor(ctl);
ctl.LostFocus -= ctl_LostFocus;
ctl.GotFocus -= ctl_GotFocus;
_focusColors.Remove(ctl.Handle);
}
else
{
_focusColors[ctl.Handle] = color;
if (ctl.Focused)
ApplyFocusColor(ctl);
ctl.LostFocus += ctl_LostFocus;
ctl.GotFocus += ctl_GotFocus;
}
}
void ctl_GotFocus(object sender, EventArgs e)
{
ApplyFocusColor((Control)sender);
}
void ctl_LostFocus(object sender, EventArgs e)
{
RemoveFocusColor((Control)sender);
}
void ApplyFocusColor(Control ctl)
{
_backColors[ctl.Handle] = ctl.BackColor;
ctl.BackColor = GetFocusColor(ctl);
}
void RemoveFocusColor(Control ctl)
{
Color color;
if (_backColors.TryGetValue(ctl.Handle, out color))
{
ctl.BackColor = color;
_backColors.Remove(ctl.Handle);
}
}
}
Add this code to your project, compile, and add a FocusColorProvider on your form using the designer. A new FocusColor property should appear on the property grid when you select a control, just set it to the color you want. The control's BackColor will be changed to this color when it takes focus.
You can utilize the Tag property of each control to store custom data associated with that control. Create a class MyCustomAttributes and assign objects of that type to MyControl.Tag. This won't work in the designer, of course, only at run time.
As an alternative, you could derive some classes like ExtTextBox from TextBox, ExtDropDownList from DropDrownList etc., having some additional attributes. That is a very simple and painless way of creating custom controls, which might be acceptable in your case, even if you said you don't want such a solution.
Related
I am developing custom WPF UserControl which has few Dependency Properties. One of the properties is enum flags. I am trying to check if it is possible to set this property from Designer in property grid.
Here is the property
public Letters Letter
{
get
{
return ((Letters)GetValue(LetterProperty));
}
set
{
SetValue(LetterProperty, value);
}
}
private static readonly DependencyProperty LetterProperty =
DependencyProperty.Register("LetterProperty", typeof(Letters), typeof(TestUserControl), new PropertyMetadata(new PropertyChangedCallback(OnLetterChanged)));
private static void OnLetterChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
return;
}
[Flags]
public enum Letters
{
A = 1,
B = 2,
C = 4,
D = 8
}
private void TxtBlock_Loaded(object sender, RoutedEventArgs e)
{
OutputText = "";
foreach (Letters letter in Enum.GetValues(typeof(Letters)))
{
if ((letter & Letter) != 0)
{
OutputText += letter + Environment.NewLine;
}
}
}
Setting multiple flags from XAML works fine and I am getting proper value set.
<Border BorderThickness="2" BorderBrush="Black">
<local:TestUserControl Letter="A,B,C"/>
Main Window
But I want to be able to set multiple choices via Properties Grid. Currently grid is listing all enum values as a drop down menu.
Properties Grid
There seems to be solutions for Windows Forms by deriving from UITypeEditor. I am looking for ways of implementing this in WPF without using 3rd party libraries and Frameworks (Telerik, XCEED etc.)
Thanks!
It is not recommended to use enumerations because enumeration is not good for property notifications. Use the int type of attribute to agree to what each number represents.
I have just started using gmap.net and I was looking for the functionality of adding labels under the markers. I see there's tooltips but I would like to have a constant label under my marker with a one word description.
I searched for docs or other answers but I cannot find anything which leads me to believe that it is not implemented. If someone can verify this I would appreciate it.
You need to create your own custom marker.
Based on the source of GMapMarker and the derived GMarkerGoogle I came up with this simplified example:
public class GmapMarkerWithLabel : GMapMarker, ISerializable
{
private Font font;
private GMarkerGoogle innerMarker;
public string Caption;
public GmapMarkerWithLabel(PointLatLng p, string caption, GMarkerGoogleType type)
: base(p)
{
font = new Font("Arial", 14);
innerMarker = new GMarkerGoogle(p, type);
Caption = caption;
}
public override void OnRender(Graphics g)
{
if (innerMarker != null)
{
innerMarker.OnRender(g);
}
g.DrawString(Caption, font, Brushes.Black, new PointF(0.0f, innerMarker.Size.Height));
}
public override void Dispose()
{
if(innerMarker != null)
{
innerMarker.Dispose();
innerMarker = null;
}
base.Dispose();
}
#region ISerializable Members
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
protected GmapMarkerWithLabel(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endregion
}
Usage (assuming a GMap instance named gm):
GMapOverlay markerOverlay = new GMapOverlay("markers");
gm.Overlays.Add(markerOverlay);
var labelMarker = new GmapMarkerWithLabel(new PointLatLng(53.3, 9), "caption text", GMarkerGoogleType.blue);
markerOverlay.Markers.Add(labelMarker)
I'll answer here because this is the first question that pops up when looking to display a text marker for the WPF GMAP.NET library. Displaying a text marker with the WPF version of the library is actually much easier than in WinForms, or at least than the accepted answer.
The GMapMarker in WPF has a Shape property of type UIElement, which means you can provide a System.Windows.Controls.TextBlock object to display a text marker :
Markers.Add(new GMapMarker(new PointLatLng(latitude, longitude))
{
Shape = new System.Windows.Controls.TextBlock(new System.Windows.Documents.Run("Label"))
});
Since the marker displays the top left portion of the shape at the given position, you can play with the GMapMarker.Offset property to adjust the text position according to its dimensions. For instance, if you want the text to be horizontally centered on the marker's position :
var textBlock = new TextBlock(new Run("Label"));
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
textBlock.Arrange(new Rect(textBlock.DesiredSize));
Markers.Add(new GMapMarker(new PointLatLng(request.Latitude, request.Longitude))
{
Offset = new Point(-textBlock.ActualWidth / 2, 0),
Shape = textBlock
});
The solution to get the TextBlock's dimensions was quickly taken from this question, so if you need a more accurate way of getting the block's dimensions to play with the offset I suggest you start from there.
I am trying to set the tab index of two UIElements within a user control. The user control contains a text box and button. I have focus currently being applied to the textbox via an attached property however I would like to have the ability to press the tab key and navigate from the textblock to the button or detect the key press (Enter key) and trigger the command on the button(I know separate question)
The main focus is accomplishing the tab index first.
Thanks for any pointers or suggestions.
UPDATE
I've since tried to employ an attached property to handle the tabbing order
public static DependencyProperty TabIndexProperty = DependencyProperty.RegisterAttached("TabIndex", typeof(int), typeof(AttachedProperties), null);
public static void SetTabIndex(UIElement element, int value)
{
Control c = element as Control;
if (c != null)
{
RoutedEventHandler loadedEventHandler = null;
loadedEventHandler = new RoutedEventHandler(delegate
{
HtmlPage.Plugin.Focus();
c.Loaded -= loadedEventHandler;
c.Focus();
});
c.Loaded += loadedEventHandler;
}
}
However when this I attempt to compile I receive errors that the TabIndex property does not exist for the button control. Any ideas why this is failing?
This is a view specific concern and, as such, even in MVVM should be handled at the ViewLevel. MVVM doesn't stipulate that you remove all code from code behind. It simply means you should have a view specific concern when you do put code there. This is one of those cases, imo.
It is late in the day... I resolved this using an attached property. in the above solution I had copied an earlier DP that I created and did not change the code before I tested.
Below is the working solution
I created a attached properties class and then added the following code:
#region Search Field Focus
public static DependencyProperty InitialFocusProperty = DependencyProperty.RegisterAttached("InitialFocus", typeof(bool), typeof(AttachedProperties), null);
public static void SetInitialFocus(UIElement element, bool value)
{
Control c = element as Control;
if (c != null && value)
{
RoutedEventHandler loadedEventHandler = null;
//set focus on control
loadedEventHandler = new RoutedEventHandler(delegate
{
HtmlPage.Plugin.Focus();
c.Loaded -= loadedEventHandler;
c.Focus();
});
c.Loaded += loadedEventHandler;
}
}
public static bool GetInitialFocus(UIElement element)
{
return false;
}
#endregion
#region Tabbing Order of Elements
public static DependencyProperty TabIndexProperty = DependencyProperty.RegisterAttached("TabIndex", typeof(int), typeof(AttachedProperties), null);
public static void SetTabIndex(UIElement element, int value)
{
element.SetValue(TabIndexProperty, value);
}
public static int GetTabIndex(UIElement element)
{
return (int)element.GetValue(TabIndexProperty);
}
#endregion
The first DP sets the focus of a textblock so that when the user control is loaded you see the cursor placed within the text field.
DP 2 sets the tabbing order. Since the focus is already applied to the current control tabbing falls into place normally. If you did not have focus on the control you would need to set this first.
then finally within the xaml declare your class in the xmlns and add away to the controls.
I have a reusable usercontrol that uses a few commands and corresponding keyboard gestures,
(specifically Escape and Ctrl+1...Ctrl+9)
Now as I use this usercontrol in multiple locations I'd like to define the input gestures in the usercontrol, which works fine as long as the focus is within the usercontrol. However, I'd need it to work as long as focus is within the current page/window.
How can I do it, or do I really have to do command/input bindings on every page?
You could handle the Loaded event of the UserControl and walk up the logical tree to find the owning page/window, then you can add the bindings there.
e.g.
public partial class Bogus : UserControl
{
public Bogus()
{
Loaded += (s, e) => { HookIntoWindow(); };
InitializeComponent();
}
private void HookIntoWindow()
{
var current = this.Parent;
while (!(current is Window) && current is FrameworkElement)
{
current = ((FrameworkElement)current).Parent;
}
if (current != null)
{
var window = current as Window;
// Add input bindings
var command = new AlertCommand();
window.InputBindings.Add(new InputBinding(command, new KeyGesture(Key.D1, ModifierKeys.Control)));
}
}
}
I want to have a TextBox which Height grows as Iam entering lines of Text.
I've set the Height property to "Auto", and so far the growing works.
Now I want that the TextBox's Height should be at least 5 lines.
Now I've set the MinLines property to "5" but if I start the app the TextBox's height is still one line.
Try setting the MinHeight property.
A hack to make the MinLines property work
public class TextBoxAdv : TextBox
{
bool loaded = false;
/// <summary>
/// Constructor
/// </summary>
public TextBoxAdv()
{
Loaded += new RoutedEventHandler( Control_Loaded );
SetResourceReference( StyleProperty, typeof( TextBox ) );
}
void Control_Loaded( object sender, RoutedEventArgs e )
{
if( !loaded )
{
loaded = true;
string text = Text;
Text = "Text";
UpdateLayout();
Text = text;
}
}
}
I propose a different solution that properly respects the MinLines property, rather than forcing you to use MinHeight.
First, start with a convenience method to allow you to Post an action to the window loop. (I'm including both one where you need to pass state and one where you don't.)
public static class Globals {
public static void Post(Action callback){
if(SynchronizationContext.Current is SynchronizationContext currentContext)
currentContext.Post( _ => callback(), null);
else{
callback();
}
}
public static void Post<TState>(TState state, Action<TState> callback){
if(SynchronizationContext.Current is SynchronizationContext currentContext)
currentContext.Post(_ => callback(state), null);
else{
callback(state);
}
}
}
Next, create an extension method for TextBox to 'initialize' the proper size based on MinLines. I put this in a Hacks class because to me, that's what this is and it clearly identifies the code as such.
public static void FixInitialMinLines(this TextBox textBox) {
Globals.Post(() => {
var textBinding = textBox.GetBindingExpression(TextBox.TextProperty)?.ParentBinding;
if (textBinding != null) {
BindingOperations.ClearBinding(textBox, TextBox.TextProperty);
textBox.UpdateLayout();
BindingOperations.SetBinding(textBox, TextBox.TextProperty, textBinding);
}
else {
var lastValue = textBox.Text;
textBox.Text = lastValue + "a";
textBox.UpdateLayout();
textBox.Text = lastValue;
}
});
}
The above code handles both bound and unbound TextBox controls, but rather than simply changing the value like other controls which may cascade that change down through the bindings, it first disconnects the binding, forces layout, then reconnects the binding, thus triggering the proper layout in the UI. This avoids unintentionally changing your bound sources should the binding be two-way.
Finally, simply call the extension method for every TextBox where MinLines is set. Thanks to the Post call in the extension method, You can call this immediately after InitializeComponent and it will still be executed after all other events have fired, including all layout and the Loaded event.
public partial class Main : Window {
public Main() {
InitializeComponent();
// Fix initial MinLines issue
SomeTextBoxWithMinLines.FixInitialMinLines();
}
...
}
Add the above code to your 'library' of functions and you can address the issue with a single line of code in all of your windows and controls. Enjoy!