I'm trying to find a way to set the visibility of a Visual element based on the completion of an loading and display state of Image Element.
Is there a property I can bind to on an Image that will indicate display state?
Note: The app is using MVVM so I would like to avoid code behind if at all possible.
You could avoid the code behind by creating a behavior that hooks into the events and sets the visibility. Then you can attach this behavior to your images using xaml.
You will need to use the ImageOpened and ImageFailed events and then set the visibility in code behind.
There is no member of the Image control that will give you the state of the image.
Related
What is the difference between Custom Control and Custom Behavior?
Where Custom Control should be used and where Custom Behavior should be. in what ways they can be best used?
Behaviour extends control functionalities
Custom controls customize the visual of Control
A behaviour can be used to extend the functionality of a control to do something that it cannot do on its own without having to modify or re-implement the entire control.
Consider for example the built-in TreeView control in WPF. It has a SelectedItem property that is read-only which means that you cannot two-way bind it to a property of your view model. If you don't want to implement your own custom TreeView control from scratch just because of this - which you probably don't :) - you could solve this by implementing a custom behaviour that sets the value of your source property whenever the value of the target property changes and vice versa. Please refer to the following blog post for more information about this and an example: https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/
The following article should also provide a good introduction to attached behvaiours: https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF
So a behaviour is basically a piece of code that can be attached to some element in the XAML markup of a view through attached properties and add additional functionality to this element.
A control is a UI component that encapsulates some functionality and has a template such, as for example a Button or a ListBox.
Behaviors encapsulate pieces of functionality into a reusable component.
Custom controls are more work than custom behaviors(White box vs black box)
Some things you have to do a custom control, eg Access Protected members.
Hi I have an image of a '-' to the left of my slider, and an image of a '+' to the right.
How do I bind these images such that if they are clicked on they will increase or decrease the slider by my value of SMALLCHANGE?
Thanks.
Mind that the easiest way is to just name your Slider, handle the Image.MouseLeftButtonUp in the UserControl and change its value in code behind. If you doesn't plan to reuse this behavior, there is no need to do any further.
To make this behavior reusable, there are several approaches. Initially I'd suggest using a EventTrigger in the image with a CallMethodAction bound to the Slider, but the Slider does not has methods like LargeIncrement, SmallIncrement, LargeDecrement, SmallDecremenet. You could subclass it and implement these methods yourself, though.
Another option is to create an AttachedProperty like "IncrementSlider" to the Image (or any FrameworkElement), which would receive a Slider instance through a ElementName binding. In the PropertyChangedCallback (see this) of the PropertyMetadata, you could subscribe to the MouseLeftButtonUp event of the control and modify the Slider. You could create other attached properties as well, like "DecrementSlider", "SmallIncrementSlider", as well.
A better solution than the AttachedProperty in this case would be an AttachedBehavior. You can create a behavior to a FrameworkElement (or just Image) with a Slider property and other control properties, like "IncrementOrDecrement" and "SmallOrLarge". In the behavior's OnAttached method you would subscribe to the MouseLeftButtonUp of the control to update the Slider (if there is one available). You could then attach the behavior to the Images, bind the Slider property of the behavior to the desired Slider, and configure it to each of them, to increment or decrement in large or small steps.
In Silverlight for Windows phone, I need to go to a specific place in a page by code (hopefully with animation). The requirement is a bit like navigate to a specific anchor in HTML using url such as http://url#anchorname. However, I don't know how to do it in Windows Phone. Any ideas?
What do you mean by "go"? You could try setting the focus on a control calling control.Focus(). If you want to just scroll a ScrollViewer or a ListBox to display a control that is inside of it - WPF had a BringIntoView() method that some people ported to Silverlight, eg. here.
If you want animation - you will need to add an attached dependency property that will allow to run an animation updating the scroll offset using ScrollToVerticalOffset(). Then have the BringIntoView implementation - instead of just jumping to the offset using ScrollToVerticalOffset - run an animation that will update the attached dependency property and smoothly animate calling ScrollToVerticalOffset many times.
I want to show all the ToolTips on a view as soon as it's opened. That's fine, I can set the ToolTip.IsOpen properties to true. That certainly shows the ToolTips, the problem is they all show up at (0, 0). Apparently the positioning of the ToolTips doesn't happen until its target control is hovered. Only after hovering are the ToolTips positioned correctly. Setting focus to all the controls first doesn't help either; the target control has to receive the hover event. I'm assuming the ToolTip hooks into the hover event of its target and positions itself inside this event. Is there a protected method I can call in a ToolTip subclass or some such?
ToolTipService.SetPlacement(button1,
System.Windows.Controls.Primitives.PlacementMode.Top);
See this MSDN Page to see all the placement options
EDIT
See this article about the custom positioning of tooltips. It refers to a project on codeplex that contains the code: SmartToolTips
As far as I can tell what I'm asking here is not possible beyond manually doing layout or customizing the source. I implemented a completely custom tooltip instead. sigh
What is considered the best way of enabling or disabling multiple controls in Silverlight at the same time (textbox, combobox, autocompletebox and the like)?
I suppose I could bind the "IsEnabled" property of each control to a boolean property. That property only exists for interactive controls and not textblocks.
I could loop through the children recursively and set their properties appropriately, but that seems inelegant.
Ideally, I'd like to just set some disable-like property on the parent container of the controls, giving even the TextBlocks a disabled look similar to a Windows form.
Is there a way to just disable the parent container?
Use the ContentControl Silverlight provides.
<ContentControl x:Name="GroupOfControls" >...Your controls...</ContentControl>
//Enable and Disable
GroupOfControls.IsEnabled = false;
You could use a ViewModel approach similar to the answer in StackOverflow 1545844
By having a calculated IsEnabled property you can then bind the elements in the View which should be controled by this property.
Usually I always create a ControlHandler Class that does all the updates on my controls. (Just to separate concerns)
Recently we had to reset all controls on the form and didn't want to loop through every single control.
All control-related data logic gets updated in the ControlHandler class.
We then only apply the values appropriate values / properties onto our controls.
This is a workaround but worked pretty well and also cleanly for us.
There are, of course, better ways to solve that..
I was looking into disabling multiple controls when fetching data from a web service.
The BusyIndicator control got me what I needed with very little effort.
Maybe it'll be a good enough solution for others as well.
Wrap with UserControl and set its IsEnabled property.
...