WPF - Dynamic tooltip - c#

I have a class ToolTipProvider
which has a method
string GetToolTip(UIElement element)
which will return a specific tooltip for the UIElement specified, based on various factors including properties of the UIElement itself and also looking up into documentation which can be changed dynamically. It will also probably run in a thread so when the form first fires up the tooltips will be something like the visual studio 'Document cache is still being constructed', then populated in the background.
I want to allow this to be used in any wpf form with the minimum effort for the developer. Essentially I want to insert an ObjectDataProvider resource into the Window.Resources to wrap my ToolTipProvider object, then I think I need to create a tooltip (called e.g. MyToolTipProvider) in the resources which references that ObjectDataProvider, then on any element which requires this tooltip functionality it would just be a case of ToolTip="{StaticResource MyToolTipProvider}"
however I can't work out a) how to bind the actual elemnt itself to the MethodParameters of the objectdataprovider, or b) how to force it to call the method each time the tooltip is opened.
Any ideas/pointers on the general pattern I need? Not looking for complete solution, just any ideas from those more experienced

Create a new user control which functions as a tool-tip view factory.
Use your control as the tool-tip, passing any data you need for the factory to your control using binding (e.g. the data, the containing control, ...)
<AnyControl>
<AnyControl.ToolTip>
<YourToolTipControl Content="{Binding}" />
</AnyControl.ToolTip>
</AnyControl>

Not calling myself an expert, but I'd probably attempt such a feature with an attached property. This would be attachable to any element in your UI and you can specify an event handler that gets access to the object to which the property is being attached as well as the value passed to the attached property. You can keep a reference to the element to which your attached property was attached and you would then be able to change the ToolTip whenever you want.

Related

How do you bind 2 controls?

In my app, there is a feature to customize it's own Controls (like Textbox, Labels, Textblocks, Buttons, etc), this feature interface is located inside a TabItem. Lets say i want to modify Button A, which is located in ANOTHER container. On the feature interface, i set it's Foreground property to White, at this moment i don't know whether the Button looks better or not, so i have to go to the container which contains that Button.
What am i trying to do is, i want to create a "preview" Control (which is the same type as the actual target) inside the feature interface. I want any changes on this "preview" control are reflected to the actual target Control. With this, i won't need to navigate to where the target Control located.
When i used the title ("How do you bind 2 controls)" with google, all results actually gives me "how to bind SINGLE property of a control to another control's property". What i want is how do you bind/link 2 Controls literally, i mean, i want to bind ALL properties of Control A to ALL properties of Control B.
Binding them one by one is one (tiring) way. Is there another way to achieve this?
I would prefer code-behind method.
There is no "fast" way to do this, you will have to bind one by one according to your buisness logic.
Also a binding is not cheap regarding performance so binding each and every property of a control even those you dont explicitly need, is a warning sign.

Conditionally validate Dynamically generated Controls in Silverlight

I am having a form with different type of controls like Text Box, Drop downs, Check box, Radio buttons etc. All these controls are loaded dynamically from database at run time.
I want to perform validation on Text box on conditional basis. For example, If we have selected any value in drop down, then you must have to fill details in Text box. Otherwise text box details are not required.
I am open to use database to perform this task and I am using MVVM pattern in my project.
Any help on this is highly appreciated.
Thanks.
(I started this as a comment, but it ended up being too long).
In theory you have access to all these controls and their values in your ViewModel.
Without knowing the specifics of your program, it's difficult to suggest anything useful, but in essence you need to expose some more properties from your ViewModel (probably boolean) which will be calculated based on the values in your controls. Then you need to bind IsEnabled properties on your controls to these new properties.
Sounds simple, but I think you have some architectural problems which will make it difficult to implement what I suggested above. In order for this to work and automatically update your controls whenever other controls' content change, your ViewModel needs to implement INotifyPropertyChanged and raise PropertyChanged event every time you update one of those boolean properties.
I think what you're trying to do could be achieved with ItemsControl and DataTemplates (and maybe DataTemplateSelectors). This will allow you to store "data" in your ViewModel (say List or something more specific) without referencing the actual Controls and the relevant DataTemplates will add the right controls for different data types you have in your ViewModel.

WPF UserControl pre-configuration

I'm designing an user control and I'd like to make its behaviour configurable - but just once, when it's created. I don't need it to adapt later on, since I know beforehand that a specific window will to use it with a specific configuration.
Consider this simple markup:
<MyControl SomeProperty="True" SomeOtherProperty="12345" />
SomeProperty and SomeOtherProperty are DependencyProperties declared in my codebehind.
The issue is: The control does some preprocessing of its input data in its constructor, before InitializeComponent() is called. On that stage, I don't have the access to SomeProperty or SomeOtherProperty defined by the user - they still have the default values.
After that, if these properties are set in the XAML, they're assigned the values after the constructor. I can respond to them by introducing a PropertyChangedCallback and performing the calculations over again after each property is updated.
This is sub-optimal since I just want to pass the values once and make sure that the control's initialization logic is only ran once too - already with correct settings. The solution with PropertyChangedCallbacks requires me to make this control more complex, i.e. responsive to any changes to these dependency properties during the control's whole lifetime. This is much more than I need - it would be satisfactory for my properties to be read-only and set only once at the moment of control creation.
How can I manage to do that while keeping the XAML markup clean?
Your control must be constructed in order for WPF to set the properties - there is no way to "delay" the construction until after the properties are set.
Instead of putting your initialization logic in the constructor, you might want to try putting it elsewhere, such as subscribing to the Loaded event and initializing there. This will happen after the properties are set.

Generic slideshow getting started

This is a getting started question about how to create a reusable wpf slideshow control:
that displays a sequence of any visualizable elements e.g. a series of Image controls or a series of UserControls (should I target ContentControl, or is there a broader type that encompasses more visualizables/controls?)
the control should be able to accept an IList of some kind, which would be the elements/slides to present
the control should expose an Interval property that determines the duration of each slide, but i dont even know the basics of how to get started with that in terms of offering that property to be configured in xaml?
and what should the container be, if any, for the individual slides/controls that are passed in?
To start with, you should probably create a UserControl which contains an Image control, and perhaps Next/Previous Buttons, and anything else you may need. These would all be laid out as normal using a variety of panels, you could probably style most of it with just a Grid.
After that, your UserControl will implement the ImageSource (your IList, or IEnumerable of images), and your interval as dependency properties. These are then settable in XAML.
You would then write the logic which loads the next image and sets it as the Image's Source property, this could happen in the change event for the ImageSource property. You can then get as advanced as you wish with Image preloading/caching etc.
I've just delved into WPF myself for a "Slideshow" like project where I'm showing customer order numbers on screen for a period of time before showing the next, and using Effect/Transitions/Storyboards to move to the next frame. I found a good article on CodeProject
I used a Grid with 2 rows:
Contains my "Changing area".
Contains static information (logo, controls etc).
Rememeber to set "cliptobounds = true" on your changing area if you use any sort of transforms on it. (I know you said you aren't using transitions initially, but once people see it, they'll be asking).
Dependency properties are also easily built in C# if you just type propdp and hit tab.

What is the use of Tag property in Tree view control C#?

What is the use of Tag property in Tree view control C#?
How can we work with it?
A common use for the Tag property is to store data that is closely associated with the control (from MSDN). Any type derived from the Object class can be assigned to this property.
It's a cheap way of avoiding inheritance to add just one Property.
Every control that inherits from Control in winform has a Tag property where you can store metadata for later use, for example you can store database id in that property for every item and load data from database on tree node click
As ArsenMkrt said, every control that inherits from Windows.Forms.Control has the Tag property. This is of type System.Object, so you can store anything you want.
The idea of the Tag property probably comes from VB6, which also has this, but in VB6 it is limited to String values.
When writing a UI, sooner or later you will find yourself handing an event in which you know the UI control that the event came from, but you also need to know what backing data that control is associated with. Usually, that problem can be solved with data binding, but not always. In the latter case, you can manually populate the Tag property with whatever you need to make the code work.

Categories