Can I create a ContentTemplate and apply the template to a ContentControl ?
The following code shows how the content template can be created and applied to a content control. [XAML] <DataTemplate x:Key=’template1′> <TextBlock Text='{Binding}’ FontSize=’12’ FontWeight=’Bold’ TextWrapping=’Wrap’></TextBlock> </DataTemplate> The above code can be applied as follows. [XAML] <Button ContentTemplate='{StaticResource template1}’ Content=’Click’>
How do I display items in a ListBox horizontally?
Items can be displayed horizontally using the ItemsPanel property of the ListBox. The following lines of code are used to display the items horizontally. [XAML] <ListBox Height=’40’ Width=’50’> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation=’Horizontal’/> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBoxItem>1 ITEM 1</ListBoxItem> <ListBoxItem>2 ITEM 2</ListBoxItem> <ListBoxItem>3 ITEM 3</ListBoxItem> </ListBox>
How can I set the text as ‘Completed’ in a progress bar at the end of progress?
This can be done using the code given below. [XAML] <Page xmlns=’http://schemas.microsoft.com/winfx/2006/xaml/presentation’ xmlns:x=’http://schemas.microsoft.com/winfx/2006/xaml’ > <!– For demonstration purposes, animate ProgressBar.Value –> <Page.Triggers> <EventTrigger RoutedEvent=’Page.Loaded’> <BeginStoryboard> <Storyboard TargetName=’ProgressBar’ TargetProperty=’Value’> <DoubleAnimation From=’0’ To=’100’ Duration=’0:0:1’ /> </Storyboard> </BeginStoryboard> </EventTrigger> </Page.Triggers> <Grid Height=’30’ > <ProgressBar Name=’ProgressBar’ /> <Viewbox> <TextBlock> <TextBlock.Style> <Style TargetType=’TextBlock’> <!– Make the text ‘Loading …’ by default–> <Setter Property=’Text’ Value=’Loading …’ /> <!– But when ProgressBar.Value is 100, change the text to ‘Complete’ –> <Style.Triggers> <DataTrigger Binding=’{Binding Value, ElementName=ProgressBar}’ Value=’100’> <Setter Property=’Text’ Value=’Complete’ /> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </Viewbox> </Grid> </Page>
How can I make a TextBlock editable when it is clicked and the value is committed when ESC or ENTER is pressed?
[XAML] <StackPanel MouseLeftButtonDown=’HandleMouseInput’ Margin=’5′> <StackPanel.Resources> <!–Base TextBox Style–> <Style x:Key=’TextBoxBaseStyle’ TargetType='{x:Type TextBox}’> <Setter Property=’FontSize’ Value=’20’/> <Setter Property=’Margin’ Value=’5’/> </Style> <!–Simplified TextBox Style with template & necessary bindings.–> <Style x:Key=’BasicTextBox’ TargetType='{x:Type TextBox}’ BasedOn='{StaticResource TextBoxBaseStyle}’> <Setter Property=’Template’> <Setter.Value> <ControlTemplate TargetType='{x:Type TextBox}’> <Border BorderBrush='{TemplateBinding BorderBrush}’ BorderThickness='{TemplateBinding BorderThickness}’ Background='{TemplateBinding Background}’> <TextBlock Margin=’3.85,2,3.85,2′ Text='{TemplateBinding Text}’ TextDecorations='{TemplateBinding TextDecorations}’/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Setter Property=’Focusable’ Value=’false’/> </Style> </StackPanel.Resources> <TextBox Width=’100′ Height=’30’ Style='{StaticResource BasicTextBox}’></TextBox> </StackPanel> The events of the above code are given below. [C#] void HandleMouseInput(object sender, MouseEventArgs args) { TextBox tb = args.Source as TextBox; if (tb == null) return; if (ActiveTextBox == null) Activate(tb); else Deactivate(ActiveTextBox); } //Deactivates the active TextBox if the Enter // or ESC key is pressed. void HandleKeyInput(object sender, KeyEventArgs args) { TextBox tb = args.Source as TextBox; if (tb != null && (args.Key == Key.Return || args.Key == Key.Escape)) Deactivate(tb); } //Deactivate the Textbox the active TextBox. void HandleLostFocus(object sender, EventArgs args) { TextBox tb = sender as TextBox; if (tb != null) Deactivate(tb); } //Activate the TextBox by applying the base style //(thereby removing the basic style.) Set focus, select //all the content & invalidate the visual. //Also, dynamically add handlers for losing focus and //handling key input. void Activate(TextBox tb) { tb.Style = (Style)tb.FindResource(‘TextBoxBaseStyle’); tb.Focus(); tb.SelectAll(); tb.InvalidateVisual(); tb.LostFocus += new RoutedEventHandler(HandleLostFocus); tb.KeyDown += new KeyEventHandler(HandleKeyInput); ActiveTextBox = tb; } //Deactivate the TextBox by applying the Basic style. //Remove the event handlers. void Deactivate(TextBox tb) { tb.Style = (Style)tb.FindResource(‘BasicTextBox’); tb.LostFocus -= new RoutedEventHandler(HandleLostFocus); tb.KeyDown -= new KeyEventHandler(HandleKeyInput); ActiveTextBox = null; }
What are the uses of BindingModes?
The most common binding scenarios involve the data getting updated from the source to the target. But, in some cases, the source has to be updated when changes are made by the users in the target. Such cases can be defined using the ‘BindingModes’ of the Binding markup extension class. ‘BindingModes’ is an enumeration property and it takes the following values. 1. OneWay – The target is updated whenever the source is updated. 2. TwoWay – A change to either the target or source updates the other. 3. OneWayToSource – It is an opposite of OneWay. The source is updated whenever the target value changes. 4. OneTime – It is similar to OneWay except that the changes to the source are also not reflected in the target. The target contains the value of the source when Binding was initiated.