How to pass a parameter to a function using ObjectDataProvider ?
A parameter can be passed to a function using the ‘MethodParameters’ of the ‘ObjectDataProvider’ class. The following XAML code snippet demonstrates how to pass parameters to a function using an ObjectDataProvider. [XAML] <ObjectDataProvider ObjectInstance='{StaticResource odp1}' MethodName='WeightOnPlanet' x:Key='odp2?> <ObjectDataProvider.MethodParameters> <system:Double>95</system:Double> </ObjectDataProvider.MethodParameters> </ObjectDataProvider>
How do I update the source as I type in a TextBox?
The source can be updated using the ‘UpdateSourceTrigger’ property of the TextBox. By default, the ‘UpdateSourceTrigger’ property is set to ’LostFocus’, i.e. the source is updated when the data-bound TextBox loses the focus. By setting the ‘UpdateSourceTrigger’ property to ’PropertyChanged’, the source will be updated as you type in a TextBox. In the following code snippet a TextBox’s ‘Text’ property is bound with the Button’s ‘Width’ property. On changing the Text in the TextBox, the Width of the Button is updated. [XAML] <Button Name='hellobtn' Content='Hello' Width='150' Height='30'/> <TextBox Text='{Binding ElementName=hellobtn, Path=Width, Height='20' UpdateSourceTrigger=PropertyChanged}'/>
Can ItemsControl’s Items and ItemsSource properties be modified simultaneously ?
ItemControl’s items and ItemsSource can’t be modified simultaneously. ItemControl’s ‘Items’ property is used to add items manually and ‘ItemsSource’ property is used to bind the ItemControl with the datasource. You must not mix these techniques, simply for the reason that ItemsSource can only be set when there are no items in the Item collection and Items can only be set when the ItemsSource is set to null. Otherwise you’ll get an ‘InvalidOperation’ Exception. Note that regardless of which method you use to set items in an ItemControl, you can always retrieve items using the ItemsCollection.
How do I bind an Adorner to an Element ?
To bind an adorner to a particular UI element, the following things needs to be done. Call the static method ‘GetAdornerLayer()’, to get the AdornerLayer associated with the UIElement. Call the ‘Add()’ method to bind the Adorner to the target UIElement. [C#] AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(myTextBox); adornerLayer.Add(new ControlAdorner(myTextBox));
How to add a custom adorner ?
The common way of implementing the rendering of visual element is to override the ‘OnRenderSizeChanged’ method and use one or more ‘DrawingContext’ objects to render the adorner’s visuals as needed. Given below is a simple code snippet for adding rounded corners around the adorned element. [C#] protected override void OnRender(DrawingContext drawingContext) { Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize); // Some arbitrary drawing implements. SolidColorBrush renderBrush = new SolidColorBrush(Colors.Green); renderBrush.Opacity = 0.2; Pen renderPen = new Pen(new SolidColorBrush(Colors.Navy), 1.5); double renderRadius = 5.0; // Draw a circle at each corner. drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius); drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius); }