I have a PropertyGrid control in WinForms (http://msdn.microsoft.com/en-us/library/aa302326.aspx). Now I want to move the middle vertical line more to the left (It is always centered, but my keys are very short, while the values are Paths, which are long. The control places the line in the middle by default, eventhough the user can move it. In respect to user friendlyness, I would like to move the line more to the left programmatically. I have now searched both the WinForms designer properties as well as the members of the PropertyGrid control multiple times and have not found the option (nor any events concerning it).
Is it hidden from sight/modification by being private? Have I simply overseen it? (In that case, I am sincerely sorry) or how can I do this otherwise?
Yes, unfortunately this requires some reflection based hacks in order to be achieved.
Here is a sample extensions class:
PropertyGridExtensionHacks.cs
using System.Reflection;
using System.Windows.Forms;
namespace PropertyGridExtensionHacks
{
public static class PropertyGridExtensions
{
/// <summary>
/// Gets the (private) PropertyGridView instance.
/// </summary>
/// <param name="propertyGrid">The property grid.</param>
/// <returns>The PropertyGridView instance.</returns>
private static object GetPropertyGridView(PropertyGrid propertyGrid)
{
//private PropertyGridView GetPropertyGridView();
//PropertyGridView is an internal class...
MethodInfo methodInfo = typeof(PropertyGrid).GetMethod("GetPropertyGridView", BindingFlags.NonPublic | BindingFlags.Instance);
return methodInfo.Invoke(propertyGrid, new object[] {});
}
/// <summary>
/// Gets the width of the left column.
/// </summary>
/// <param name="propertyGrid">The property grid.</param>
/// <returns>
/// The width of the left column.
/// </returns>
public static int GetInternalLabelWidth(this PropertyGrid propertyGrid)
{
//System.Windows.Forms.PropertyGridInternal.PropertyGridView
object gridView = GetPropertyGridView(propertyGrid);
//protected int InternalLabelWidth
PropertyInfo propInfo = gridView.GetType().GetProperty("InternalLabelWidth", BindingFlags.NonPublic | BindingFlags.Instance);
return (int)propInfo.GetValue(gridView);
}
/// <summary>
/// Moves the splitter to the supplied horizontal position.
/// </summary>
/// <param name="propertyGrid">The property grid.</param>
/// <param name="xpos">The horizontal position.</param>
public static void MoveSplitterTo(this PropertyGrid propertyGrid, int xpos)
{
//System.Windows.Forms.PropertyGridInternal.PropertyGridView
object gridView = GetPropertyGridView(propertyGrid);
//private void MoveSplitterTo(int xpos);
MethodInfo methodInfo = gridView.GetType().GetMethod("MoveSplitterTo", BindingFlags.NonPublic | BindingFlags.Instance);
methodInfo.Invoke(gridView, new object[] { xpos });
}
}
}
To move the splitter position use the MoveSplitterTo extension method.
Use the GetInternalLabelWidth extension method to get the actual position of the splitter. Please note that I observed that until the SelectedObject is assigned and the PropertyGrid was not shown, GetInternalLabelWidth returns (-1).
Sample use:
using PropertyGridExtensionHacks;
//...
private void buttonMoveSplitter_Click(object sender, EventArgs e)
{
int splitterPosition = this.propertyGrid1.GetInternalLabelWidth();
this.propertyGrid1.MoveSplitterTo(splitterPosition + 10);
}
Here is a method that doesn't rely on directly using private methods or reflection. It does still use undocumented interfaces though.
In .NET 4.0 the PropertyGrid.Controls collection contains 4 controls. PropertyGrid.Controls.item(2) is an undocumented PropertyGridView (same type as in answer that uses reflection). The property PropertyGridView.LabelRatio adjusts the relative widths of the columns. The range of LabelRatio looks like it is 1.1 to 9. Smaller values make the left column wider.
I know that setting LabelRatio before you initially display the control works. However I'm not sure what all you need to do to make it take effect once the control is already displayed. You can google MoveSplitterTo to find .NET source code and look at the source for PropertyGridView to get more details. The calculations and operations involving it seem somewhat complicated and I didn't analyze them in detail.
LabelRatio is initially set to 2 (ie splits the available PropertyGrid width in half). Set it to 3 for thirds, 4 for quarter.
Code requires Imports System.Reflection
Public Sub MoveVerticalSplitter(grid As PropertyGrid, Fraction As Integer)
Try
Dim info = grid.[GetType]().GetProperty("Controls")
Dim collection = DirectCast(info.GetValue(grid, Nothing), Control.ControlCollection)
For Each control As Object In collection
Dim type = control.[GetType]()
If "PropertyGridView" = type.Name Then
control.LabelRatio = Fraction
grid.HelpVisible = True
Exit For
End If
Next
Catch ex As Exception
Trace.WriteLine(ex)
End Try
End Sub
To change size of Description Pane at bottom of PropertyGrid as lines of text
Public Sub ResizeDescriptionArea(grid As PropertyGrid, lines As Integer)
Try
Dim info = grid.[GetType]().GetProperty("Controls")
Dim collection = DirectCast(info.GetValue(grid, Nothing), Control.ControlCollection)
For Each control As Object In collection
Dim type = control.[GetType]()
If "DocComment" = type.Name Then
Const Flags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic
Dim field = type.BaseType.GetField("userSized", Flags)
field.SetValue(control, True)
info = type.GetProperty("Lines")
info.SetValue(control, lines, Nothing)
grid.HelpVisible = True
Exit For
End If
Next
Catch ex As Exception
Trace.WriteLine(ex)
End Try
End Sub
Related
I am trying to reuse NumberBoxes for a GridView because having the NumberBoxes embedded directly in the GridView data template causes undesirable behavior, while reusing them does not. The problem is that I keep getting exceptions. They say "No installed components were detected" on the following line (templateRoot.FindName("NumberBox") as GridViewItem).Content = item.NumberBox; in this context
/// <summary>
/// The callback for updating a container in the GridView named CardGridView.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void UpdateGridViewContainer(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (args.Phase == 1)
{
Grid templateRoot = args.ItemContainer.ContentTemplateRoot as Grid;
CardItem item = args.Item as CardItem;
(templateRoot.FindName("NumberBox") as GridViewItem).Content = item.NumberBox;
TypedEventHandler<NumberBox, NumberBoxValueChangedEventArgs> handler =
(box, args) =>
{
if (!double.IsNaN(args.NewValue))
{
_viewModel.ChangeCount(args, item);
}
};
item.SetHandler(handler);
}
}
The exception is thrown when the Page that contains the GridView is left and renavigated to. I have tried nulling out the NumberBoxes when the page is left, but that did not work. Well, it appeared to before the issue cropped up again.
This is the code that nulls out the NumberBoxes
/// <summary>
/// Creates new NumberBoxes for when this Page is loaded again.
/// </summary>
private void ResetNumberBoxes()
{
foreach (CardItem card in CardGridView.Items.Cast<CardItem>())
{
card.ResetNumberBox();
}
CardGridView.ItemsSource = null;
CardGridView.Items.Clear();
}
ResetNumberBox is just setting the NumberBox to null and assigning a new one.
The exception details
System.Runtime.InteropServices.COMException
HResult=0x800F1000
Message=No installed components were detected. (0x800F1000)
Source=WinRT.Runtime
StackTrace:
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
at ABI.Microsoft.UI.Xaml.Controls.IContentControlMethods.set_Content(IObjectReference _obj, Object value)
An update. I have removed the GridViewItem control from the DataTemplate and tried doing the following with the same result
CardItem item = args.Item as CardItem;
Grid.SetColumn(item.NumberBox, 1);
item.NumberBox.HorizontalAlignment = HorizontalAlignment.Center;
item.NumberBox.VerticalAlignment = VerticalAlignment.Center;
(templateRoot.FindName("GridViewTemplate") as Grid).Children.Add(item.NumberBox);
I also examined a heap dump right before the line that throws, and there was only one instance of the Grid, and the NumberBox had a parent of null.
Is there a way to smoothly animate a ScrollViewers vertical offset in Windows Phone 8.1 Runtime?
I have tried using the ScrollViewer.ChangeView() method and the change of vertical offset is not animated no matter if I set the disableAnimation parameter to true or false.
For example: myScrollViewer.ChangeView(null, myScrollViewer.VerticalOffset + p, null, false);
The offset is changed without animation.
I also tried using a vertical offset mediator:
/// <summary>
/// Mediator that forwards Offset property changes on to a ScrollViewer
/// instance to enable the animation of Horizontal/VerticalOffset.
/// </summary>
public sealed class ScrollViewerOffsetMediator : FrameworkElement
{
/// <summary>
/// ScrollViewer instance to forward Offset changes on to.
/// </summary>
public ScrollViewer ScrollViewer
{
get { return (ScrollViewer)GetValue(ScrollViewerProperty); }
set { SetValue(ScrollViewerProperty, value); }
}
public static readonly DependencyProperty ScrollViewerProperty =
DependencyProperty.Register("ScrollViewer",
typeof(ScrollViewer),
typeof(ScrollViewerOffsetMediator),
new PropertyMetadata(null, OnScrollViewerChanged));
private static void OnScrollViewerChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var mediator = (ScrollViewerOffsetMediator)o;
var scrollViewer = (ScrollViewer)(e.NewValue);
if (null != scrollViewer)
{
scrollViewer.ScrollToVerticalOffset(mediator.VerticalOffset);
}
}
/// <summary>
/// VerticalOffset property to forward to the ScrollViewer.
/// </summary>
public double VerticalOffset
{
get { return (double)GetValue(VerticalOffsetProperty); }
set { SetValue(VerticalOffsetProperty, value); }
}
public static readonly DependencyProperty VerticalOffsetProperty =
DependencyProperty.Register("VerticalOffset",
typeof(double),
typeof(ScrollViewerOffsetMediator),
new PropertyMetadata(0.0, OnVerticalOffsetChanged));
public static void OnVerticalOffsetChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var mediator = (ScrollViewerOffsetMediator)o;
if (null != mediator.ScrollViewer)
{
mediator.ScrollViewer.ScrollToVerticalOffset((double)(e.NewValue));
}
}
/// <summary>
/// Multiplier for ScrollableHeight property to forward to the ScrollViewer.
/// </summary>
/// <remarks>
/// 0.0 means "scrolled to top"; 1.0 means "scrolled to bottom".
/// </remarks>
public double ScrollableHeightMultiplier
{
get { return (double)GetValue(ScrollableHeightMultiplierProperty); }
set { SetValue(ScrollableHeightMultiplierProperty, value); }
}
public static readonly DependencyProperty ScrollableHeightMultiplierProperty =
DependencyProperty.Register("ScrollableHeightMultiplier",
typeof(double),
typeof(ScrollViewerOffsetMediator),
new PropertyMetadata(0.0, OnScrollableHeightMultiplierChanged));
public static void OnScrollableHeightMultiplierChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var mediator = (ScrollViewerOffsetMediator)o;
var scrollViewer = mediator.ScrollViewer;
if (null != scrollViewer)
{
scrollViewer.ScrollToVerticalOffset((double)(e.NewValue) * scrollViewer.ScrollableHeight);
}
}
}
and I can animate the VerticalOffset property with DoubleAnimation:
Storyboard sb = new Storyboard();
DoubleAnimation da = new DoubleAnimation();
da.EnableDependentAnimation = true;
da.From = Mediator.ScrollViewer.VerticalOffset;
da.To = da.From + p;
da.Duration = new Duration(TimeSpan.FromMilliseconds(300));
da.EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseOut };
Storyboard.SetTarget(da, Mediator);
Storyboard.SetTargetProperty(da, "(Mediator.VerticalOffset)");
sb.Children.Add(da);
sb.Begin();
Mediator is declared in XAML.
But this animation is not smooth on my device (Lumia 930).
You should stick with ChangeView for scrolling animations regardless of whether data virtualization is on or not.
Without seeing your code where the ChangeView doesn't work, it's a bit hard to guess what's really going on but there are a couple of things that you can try.
First approach is to add a Task.Delay(1) before calling ChangeView, just to give the OS some time to finish off other concurrent UI tasks.
await Task.Delay(1);
scrollViewer.ChangeView(null, scrollViewer.ScrollableHeight, null, false);
The second approach is a bit more complex. What I've noticed is that, when you have many complex items in the ListView, the scrolling animation from the first item to the last (from the ChangeView method) isn't very smooth at all.
This is because the ListView first needs to realize/render many items along the way due to data virtualization and then does the animated scrolling. Not very efficient IMHO.
What I came up with is this - First, use a non-animated ListView.ScrollIntoView to scroll to the last item just to get it realized. Then, call ChangeView to move the offset up to a size of the ActualHeight * 2 of the ListView with animation disabled (you can change it to whatever size you want based on your app's scrolling experience). Finally, call ChangeView again to scroll back to the end, with animation this time. Doing this will give a much better scrolling experience 'cause the scrolling distance is just the ActualHeight of the ListView.
Keep in mind that when the item you want to scroll to is already realized on the UI, you don't want to do anything above. You simply just calculate the distance between this item and the top of the ScrollViewer and call ChangeView to scroll to it.
I already wrapped the logic above in this answer's Update 2 section (thanks to this question I realized my initial answer doesn't work when virtualization is on :p). Let me know how you go.
I think that question has already been answered here:
Animated (Smooth) scrolling on ScrollViewer
There is also the WinRT XAML Toolki, which provides "a way to scroll a ScrollViewer to specified offset with animation":
http://winrtxamltoolkit.codeplex.com/
With ScrollToVerticalOffset deprecated/obsolete in newer builds of Windows 10 (leaving the ScrollViewOffSetMediator extension control no longer working), and the new ChangeView method not actually providing smooth or controllable animation, a new solution is needed. Please see my answer here which allows one to smoothly animate and zoom the ScrollViewer and its contents to any desired position, regardless of where the application's end user has the scrollbars initially positioned:
How to scroll to element in UWP
I believe this article is what you're looking for and it seems the method he used is working for you.
Quick Way:
Add offset dependency parameter manually to scrollviewer.
Duplicate your scrollviewer
Use an animator.
I have written this code in visual studio 2013 utilizing .net v4.5. The problem I am having is that I am now having to drop down to .net v3.5 and the dynamic keyword is throwing an error as missing an assembly reference. Is there an equivalent type to 'dynamic' in .net v3.5 or a way for me to achieve the same results as below?
I thought I may have found my answer here, but var is throwing errors when I add the .Attributes or .Text property modifications.
private void CreateControl<T>(string objText,
Panel pnl,
string HTMLTag = "<td>",
string applicantID = "",
EventHandler hndl = null)
{
pnl.Controls.Add(new LiteralControl(HTMLTag));
dynamic obj = Activator.CreateInstance(typeof(T));
obj.Text = objText;
if (applicantID != string.Empty)
{
obj.Attributes.Add("ApplicantID", applicantID);
}
if (hndl != null)
{
obj.Click += new EventHandler(hndl);
}
pnl.Controls.Add(obj);
pnl.Controls.Add(new LiteralControl(HTMLTag.Insert(1, "/")));
}
Instead of trying to hack this together in some bound to fail way and since there isn't a 'dynamic' control in .net v3.5, I have instead decided to just completely forgo this method and wrote some overloads instead. This way seems safer at this point; works the same, just a bit more code...
#region CreateControl() Overloads
/// <summary>
/// Creates a LinkButton control.
/// </summary>
/// <param name="objText">.Text property of this LinkButton control.</param>
/// <param name="pnl">Panel this control will be attached to.</param>
/// <param name="hndl">Event handler attached to this LinkButton control.</param>
/// <param name="HTMLTag">Opening tag used to contain this control.</param>
private void CreateControl(string objText,
Panel pnl,
EventHandler hndl,
string HTMLTag)
{
pnl.Controls.Add(new LiteralControl(HTMLTag));
LinkButton obj = new LinkButton();
obj.Text = objText;
obj.Click += new EventHandler(hndl);
pnl.Controls.Add(obj);
pnl.Controls.Add(new LiteralControl(HTMLTag.Insert(1, "/")));
}
/// <summary>
/// Creates a Label control.
/// </summary>
/// <param name="objText">.Text property of this Label control.</param>
/// <param name="pnl">Panel this control will be attached to.</param>
/// <param name="HTMLTag">Opening tag used to contain this control.</param>
private void CreateControl(string objText,
Panel pnl,
string HTMLTag)
{
pnl.Controls.Add(new LiteralControl(HTMLTag));
Label obj = new Label();
obj.Text = objText;
pnl.Controls.Add(obj);
pnl.Controls.Add(new LiteralControl(HTMLTag.Insert(1, "/")));
}
/// <summary>
/// Creates the specified literal control.
/// </summary>
/// <param name="ControlText">HTML text containing instructions for creating the desired literal control.</param>
/// <param name="pnl">Panel this literal control will be attached to.</param>
private void CreateControl(string ControlText,
Panel pnl)
{
pnl.Controls.Add(new LiteralControl(ControlText));
}
#endregion
Is there an equivalent type to 'dynamic' in .net v3.5
No. dynamic requires .NET 4.0.
or a way for me to achieve the same results as below?
You could use reflection instead of dynamic to create the control and add your event handlers.
However, since this appears to be one of a few custom controls you're creating (given the attributes, etc), you may be able to constrain to an interface or base class, which would allow you to create the items and use those shared properties directly.
Based on your code, it looks like you're writing a generic method to pass in some unknown controls and attach them to a panel.
It also looks like you're dealing with different types of controls; i.e., not all WebControls have Text, and Attributes, AND Click properties;
This is a bit hacky but works in 3.5 - you can just use casting of the various underlying types or interfaces to access the needed properties, something like this:
private void CreateControl<T>(string objText, Panel pnl, string HTMLTag,
string applicantID, EventHandler hndl)
where T : Control, new()
{
pnl.Controls.Add(new LiteralControl(HTMLTag));
T obj = new T();
if (obj is ITextControl) (obj as ITextControl).Text = objText;
if (applicantID != string.Empty && obj is WebControl)
(obj as WebControl).Attributes.Add("ApplicantID", applicantID);
if (obj is IButtonControl)
{
(obj as IButtonControl).Text = objText;
if (hndl != null)
{
(obj as IButtonControl).Click += new EventHandler(hndl);
}
}
pnl.Controls.Add(obj as Control);
pnl.Controls.Add(new LiteralControl(HTMLTag.Insert(1, "/")));
}
Test code:
protected void Page_Load(object sender, EventArgs e)
{
var panel = new Panel();
CreateControl<Button>("test", panel, "<td>", "123", (s, args) => Console.WriteLine("hello!"));
CreateControl<Label>("test", panel, "<td>", "123", (s, args) => Console.WriteLine("hello!"));
CreateControl<Panel>("test", panel, "<td>", "123", (s, args) => Console.WriteLine("hello!"));
CreateControl<Literal>("test", panel, "<td>", "123", (s, args) => Console.WriteLine("hello!"));
//This won't compile because object doesn't fit <control> constraint
//CreateControl<object>("test", panel, "<td>", "123", (s, args) => Console.WriteLine("hello!"));
}
To be honest I'm not 100% sure I like this approach. I might use some more specific methods and possibly method overloading to get more specific with different types of control creation, but this may help point you in the right direction.
Note that optional parameters are also not yet "invented" in C# 3.0 which shipped with .net 3.5, so you have to actually pass in all of the values.
dynamic keyword is available on .net 4.x and is a simple way to store any kind of value, it just resolve his type in runtime. It has been useful to me working with JSON strings.
string jsonValue = "{name:'Pedro',lastName:'Mora'}";
dynamic Variable = new JavascriptSerializer().Deserialize<dynamic>(jsonValue);
return Variable.name;
//It will return "Pedro"
Thing is that you have to be sure that the value won't be null and the properties or attributes or methods or something refered to the object exists and it takes it's values on runtime.
I am looking for the best way to set the validation group for all of the controls contained in a user control. Most of the controls in the control are created dynamically when the control is loaded. It has quite a few input fields and validators.
I would like to save time by setting the validation for all the controls and validators with some sort of function that could just loop through everything and set it.
It seems though that there is no consistent interface that includes validation group for all of the different controls that have the property.
Should I use reflection to check for Validation group, I know I can do this, but is there a better way?
We are using C# by the way.
Any help would be appreciated. Thanks!
EDIT: I put the answer down below for anyone who wants the code.
Just wanted to mark this answered.
Here is the code I used to set the validation group. I decided to go with using reflection, since I could check types for those I know have ValidationGroup, and cast and set, but there are a lot of types to check, and it could miss new controls that may be added in the future. Would have been sweet if the ValidationGroup were part of some kind of interface that things had to implement.
/// <summary>
/// this is an extension method to iterate though all controls in a control collection
/// put this in some static library somewhere
/// </summary>
/// <param name="controls"></param>
/// <returns></returns>
public static IEnumerable<Control> All(this ControlCollection controls)
{
foreach (Control control in controls)
{
foreach (Control grandChild in control.Controls.All())
yield return grandChild;
yield return control;
}
}
/// <summary>
/// this function uses reflection to check if the validation group exists, and then set it to the
/// specified string
/// </summary>
/// <param name="ValidationGroup"></param>
private void SetValidationGroup(string ValidationGroup)
{
//set the validation group for all controls
if (ValidationGroup.IsNotNullOrEmpty())
{
foreach (Control c in Controls.All())
{
var Properties = c.GetType().GetProperties();
var vg = Properties.Where(p => p.Name == "ValidationGroup").SingleOrDefault();
if (vg != null)
{
vg.SetValue(c, ValidationGroup, null);
}
}
}
}
I recently had a very similar problem- the soloution I used was to create a couple of extension methods that could loop over all child/descendent controls of a control, find ones of a particular type and then call a subroutine on them (this subroutine could for example, set any properties of a control). The code is below in VB.Net (sorry this is what we use at work, I'm sure a code translator should be able to sort this out for you).
Public Module ControlExtensionMethods
''' <summary>
''' Gets all validation controls used by a control.
''' </summary>
''' <param name="onlyGetVisible">If true, will only fetch validation controls that currently apply (i.e. that are visible). The default value is true.</param>
''' <returns></returns>
''' <remarks></remarks>
<Extension()>
Public Function GetAllValidationControls(ByVal target As Control, Optional ByVal onlyGetVisible As Boolean = True) As ReadOnlyCollection(Of BaseValidator)
Dim validators As New List(Of BaseValidator)
GetControlsOfType(Of BaseValidator)(target, Function(x) Not onlyGetVisible OrElse x.Visible = onlyGetVisible, validators)
Return validators.AsReadOnly()
End Function
''' <summary>
''' Gets if the control is in a valid state (if all child/descendent validation controls return valid)
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
<Extension()>
Public Function IsValid(ByVal target As Control) As Boolean
Return target.GetAllValidationControls().All(Function(x)
x.Validate()
Return x.IsValid
End Function)
End Function
''' <summary>
''' Iteratively fetches all controls of a specified type/base type from a control and its descendents.
''' </summary>
''' <param name="fromControl"></param>
''' <param name="predicate">If provided, will only return controls that match the provided predicate</param>
''' <remarks></remarks>
<Extension()>
Public Function GetControlsOfType(Of T As Control)(ByVal fromControl As Control, Optional ByVal predicate As Predicate(Of T) = Nothing) As IList(Of T)
Dim results As New List(Of T)
GetControlsOfType(fromControl, predicate, results)
Return results
End Function
Private Sub GetControlsOfType(Of T As Control)(ByVal fromControl As Control, ByVal predicate As Predicate(Of T), ByVal results As IList(Of T))
'create default predicate that always returns true if none is provided
Dim cntrl As Control
If predicate Is Nothing Then predicate = Function(x) True
If fromControl.HasControls Then
For Each cntrl In fromControl.Controls
GetControlsOfType(Of T)(cntrl, predicate, results)
Next
End If
If TypeOf fromControl Is T AndAlso predicate(fromControl) Then
results.Add(fromControl)
End If
End Sub
End Module
An example of using this code to disable all validators:
Array.ForEach(myUserControl.GetAllValidationControls().ToArray(), sub(x) x.Enabled = False)
I was butting my head against this issue just now, and came up with an alternative solution that doesn't involve duck-typing:
string newGroup = "foo";
IEnumerable<BaseValidator> validators = this.Controls.OfType<BaseValidator>();
IEnumerable<Button> buttons = this.Controls.OfType<Button>();
foreach (var validator in validators)
validator.ValidationGroup = newGroup;
foreach (var button in buttons)
button.ValidationGroup = newGroup;
This also might be an alternative:
foreach (var c in this.Controls)
{
if (c is BaseValidator)
(c as BaseValidator).ValidationGroup = newGroup;
else if (c is Button)
(c as Button).ValidationGroup = newGroup;
}
I need an Image that is grayed out when disabled (IsEnabled=False). A grayed out version of the image can be produced by reading the BitmapImage into a FormatConvertedBitmap which is shown here.
I have been able to get this working with a UserControl but now I would like the same behavior in a specialized Image class for more flexibility. I don't care if this is implemented in XAML, code-behind or both, but it needs to be a subclass of Image.
The usage could be:
<DisableableImage Source="Images/image1.png" />
<DisableableImage Source="Images/image1.png" IsEnabled="False" />
<!-- Since IsEnabled is inherited down the tree,
the image will be grayed out like the rest of the button -->
<Button IsEnabled="False">
<StackPanel Orientation="Horizontal">
<TextBlock>OK</TextBlock>
<DisableableImage Source="Images/ok.png" />
</StackPanel>
</Button>
Have a look at this link
EDIT:
Or this one (all you need is the AutoGreyableImage class)
I made a little comparison based on the following solutions.
The approaches in the link provided by the OP
The links provided by Thomas Levesque
AutoDisabledImage
AutoGreyableImage
Greyscale Effect
Since I already had a licens for the Infragistics Net Advantage for WPF it was easy to try it out
Here is the result
So the best approach depends on what results you are after. As for me, I think the result produced by AutoDisabledImage from Infragistics is too bright, AutoGreyableImage does a pretty good job (Identical result to Approach 1 (OP link)) and GreyscaleEffect produces the best result.
if you use this a lot consider creating a custom Effect introduced with .NET 3.5 SP1 (not bitmapeffect) to render such an operation on your GPU. this effect can then be easily controlled by triggers.
More complete version of the AutoGreyableImage by Thomas Lebrun. For anyone interested, I started using Thomas Lebruns class and ran into a couple of nullreference exceptions, as well as finding out that an image would not be disabled if the isEnabled property was set first and the source set after.
So here's the class that finally did the trick for me. À propos, you can of course add the matter of opacity into this, but I decided to leave that up to the xaml around the image.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
namespace MyDisabledImages
{
/// <summary>
/// Class used to have an image that is able to be gray when the control is not enabled.
/// Based on the version by Thomas LEBRUN (http://blogs.developpeur.org/tom)
/// </summary>
public class AutoGreyableImage : Image
{
/// <summary>
/// Initializes a new instance of the <see cref="AutoGreyableImage"/> class.
/// </summary>
static AutoGreyableImage()
{
// Override the metadata of the IsEnabled and Source property.
IsEnabledProperty.OverrideMetadata(typeof(AutoGreyableImage), new FrameworkPropertyMetadata(true, new PropertyChangedCallback(OnAutoGreyScaleImageIsEnabledPropertyChanged)));
SourceProperty.OverrideMetadata(typeof(AutoGreyableImage), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnAutoGreyScaleImageSourcePropertyChanged)));
}
protected static AutoGreyableImage GetImageWithSource(DependencyObject source)
{
var image = source as AutoGreyableImage;
if (image == null)
return null;
if (image.Source == null)
return null;
return image;
}
/// <summary>
/// Called when [auto grey scale image source property changed].
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
protected static void OnAutoGreyScaleImageSourcePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs ars)
{
AutoGreyableImage image = GetImageWithSource(source);
if (image != null)
ApplyGreyScaleImage(image, image.IsEnabled);
}
/// <summary>
/// Called when [auto grey scale image is enabled property changed].
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
protected static void OnAutoGreyScaleImageIsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
{
AutoGreyableImage image = GetImageWithSource(source);
if (image != null)
{
var isEnabled = Convert.ToBoolean(args.NewValue);
ApplyGreyScaleImage(image, isEnabled);
}
}
protected static void ApplyGreyScaleImage(AutoGreyableImage autoGreyScaleImg, Boolean isEnabled)
{
try
{
if (!isEnabled)
{
BitmapSource bitmapImage = null;
if (autoGreyScaleImg.Source is FormatConvertedBitmap)
{
// Already grey !
return;
}
else if (autoGreyScaleImg.Source is BitmapSource)
{
bitmapImage = (BitmapSource)autoGreyScaleImg.Source;
}
else // trying string
{
bitmapImage = new BitmapImage(new Uri(autoGreyScaleImg.Source.ToString()));
}
FormatConvertedBitmap conv = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0);
autoGreyScaleImg.Source = conv;
// Create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info
autoGreyScaleImg.OpacityMask = new ImageBrush(((FormatConvertedBitmap)autoGreyScaleImg.Source).Source); //equivalent to new ImageBrush(bitmapImage)
}
else
{
if (autoGreyScaleImg.Source is FormatConvertedBitmap)
{
autoGreyScaleImg.Source = ((FormatConvertedBitmap)autoGreyScaleImg.Source).Source;
}
else if (autoGreyScaleImg.Source is BitmapSource)
{
// Should be full color already.
return;
}
// Reset the Opcity Mask
autoGreyScaleImg.OpacityMask = null;
}
}
catch (Exception)
{
// nothin'
}
}
}
}
Create a DisableableImage class that is a typical WPF control. Inside, place two elements: the image, and a rectangle that appears only when the control is disabled. The rectangle should be the same width and height as the image, and it should overlay the image. With a color of gray and an alpha of somewhere around 40%, you should get an effect similar to actually graying out the image -- without all the effort to actually modify the image itself.