I have this TextBox that has a MaxHeight value of 62 by default in my XAML file, and I want to programmatically unset it from codebehind on a certain event. Sadly, this:
myTextBox.MaxHeight = 0;
actually sets the maximum height to zero... making it invisible. Isn't there a method on UI elements to purely unset a specific property?
There is a method to reset any dependency property to its default value, without explicitly specifying (or even knowing) the value. Just call ClearValue:
myTextBox.ClearValue(FrameworkElement.MaxHeightProperty);
Note however that this clears the local value of the property. If there is any value set by a Template or Style Setter, that value will be effective then.
You can set it back to it's default value:
myTextBox.MaxHeight = double.PositiveInfinity;
This allows the text box to grow to "any height".
Related
In a drawing application that allows user to add different objects like Rectangle, Ellipse, Text, Image etc, I have a "color box" that lets user see or change the fill of selected object(s). If there are multiple objects selected, I can't obviously show all, so I show the color of first selected object. When user changes the color of "color box", I want to change the fill of all selected objects. (That's also how VS properties window works btw)
How do I bind my "color box"'s Brush property (type Brush) to do this thing?
What I have already tried is this: I bind "color box"'s Brush property to selection object and then use a Converter that examines the selection object and if there is one or more objects in it, it returns the color of first selected object in Convert() function.
Problem however is that I have no way of implementing ConvertBack(), because it doesn't give me access to the selection object, so that I could iterate through the selected objects and assign them newly selected color.
Using MultiBinding is also of no use, as value parameter of ConvertBack() is a single object and doesn't give me access to the selection object. One idea was to supply selection object using ConverterParameter, but ConverterParameter is not bindable.
Another idea (hack?) was to keep a class level variable to point to selection object and assign in Convert() function (by passing selection object in MultiBinding) and then use that variable in ConvertBack(), but I don't know how safe it is and whether Convert() and ConvertBack() will always be called in order.
So how do I achieve this?
OK. This turned out to be easier than I thought. For future readers, I simply added a property to my VM of Brush type (call it SelectionBrush) whose getter would return the Brush of first selected object, and whose setter would iterate through the entire selection and assign the Brush to appropriate objects (by checking their types). No more converters needed!
I have a WPFToolkit DataGrid with at least one column bound (via a proxy object as columns are not part of the visual tree) to a property. I wish to toggle all columns to Visible so that I can perform a calculation based on the DataGridColumnHeader (which is only created when its column is visible for the first time). Having done the calculation I want to reset the column to use the binding that was previously set.
I've attempted to get and store the Binding Expression etc, but with no joy. I have also attempted to use the DependencyObject.SetValue() method to change the property value non-destructively, but this doesn't event correctly change the value, let alone retain the original binding.
Any ideas?
You need to call SetCurrentValue() so that it won't clear the binding. SetValue destroys the old binding.
From MSDN:
This method is used by a component that programmatically sets the value of one of its own properties without disabling an application's declared use of the property. The SetCurrentValue method changes the effective value of the property, but existing triggers, data bindings, and styles will continue to work.
Given you have this
<TextBox Text="{Binding TestProperty}"/>
The SetValue you will overwrite the binding with whatever you provide. If you call SetCurrentValue, however, will ensure that the property takes on the given value, but won't destroy any bindings.
Be aware that you should not use SetCurrentValue in your dependency properties' setter/getter.
SetCurrentValue is more useful in scenarios where you need a property to take on a given value but don't want to overwrite any bindings, triggers, or styles that have been configured against your property.
I'm implementing a custom animation on a custom type (inherits from AnimationTimeline). As I'm using the animation inside VisualStateManager, I can't use the From property - so I didn't even bother to create it. But I do have a To dependency property.
Now, inside GetCurrentValue() I have three parameters, and I counted on using the first one ("defaultOriginValue") as the original value of my animated property. Apparently, it's not the original value but the default value (as specified in FrameworkProperyMetadate for the animated property).
How do I get the actual property value to animate from?
I have a WPF MVVM data form window with data validation. A lot of the controls are text boxes. Currently, the data binding trigger is set to the default, i. e. loss of focus. This means that a field is only validated when it is likely to be filled out completely. So when deleting a number and typing another number, the transient empty value will not be displayed as input error.
But a drawback is that the Save button can only be enabled when the focus moves out of the text box. (No matter where, just out of the edited control. Assuming there is anything else focusable.) If this is the only change, the user waits for the Save button to be available and nothing happens. For the Save button, I'd like to use an immediate binding trigger. How can that be done?
Edit: Forgot to mention that my Save button (which uses ICommand) is only enabled when the input is determined modified and valid. So the data will remain unmodified until data binding updates it, and that won't happen until the focus moves to another control.
I actually had a similar question a while back and the solution I ended using was a custom DependencyProperty that kicked off a timer when a key was pressed, and only actually processed the PropertyChange notification if a specific time had passed.
This means the bound property doesn't get updated (and validated) unless the user pauses in typing for a set period of times.
The code can be found here (may need a bit of cleanup), and it is used like this:
<TextBox
local:DelayedUpdateBehavior.TargetProperty="{x:Static TextBox.TextProperty}"
local:DelayedUpdateBehavior.Milliseconds="1000"
Text="{Binding MyTextProperty, UpdateSourceTrigger=Explicit}" />
Edit: Actually this link might be better. It's a markup extension so you can use it directly from your binding. I can't remember which of these two methods I used in the past, but I know it was one of them :)
<TextBox Text="{local:DelayBinding Path=MyTextProperty, Delay='00:00:01'}" />
Assuming you're using an ICommand type interface for the button click event:
You can...Implement string properties with INotifyPropertyChanged and bind them to your textbox controls. Now in your Command canexecute method you can check to see if the property is !nullorempty.
e/ grammar
Set your Binding's UpdateSourceTrigger property to PropertyChanged. The default for TextBoxes is LostFocus.
Update: So you want to have data binding working on your TextBox and only allow numbers? Have a look at this question: Create WPF TextBox that accepts only numbers
Or use a converter and bind the Save button's IsEnabled property to your TextBox (maybe using a MultiBinding if there's more than one), and use a converter which determines if the text is a valid number and returns true or false.
I would like to let users resize my form but when they reach a specific size to disable this ability to make it smaller window than i want.
Any suggestions?
You can set the MinimumSize property of the form to the minimum size you want to enforce.
There is also a mirror property MaximumSize (mentioned for completeness).
Set the MinimumSize property on your form. This will prevent the user from making the form any smaller than this value. Similarly, you can set the MaximumSize property to ensure that they cannot make it any larger than that value.
Set the form's MinimumSize property in the designer.
Set the form's MinimumSize property to the smallest size you want to allow.