How can I define a style for a ContentControl to give it a meaningful appearance in the UI ?
This can be done as follows. [XAML] <Style x:Key=’ContentCtrl’ TargetType=’ContentControl’> <Setter Property=’Background’ Value=’Red’/> <Setter Property=’Foreground’ Value=’Green’/> <Setter Property=’FontSize’ Value=’20’/> <Setter Property=’FontWeight’ Value=’Bold’/> <Setter Property=’Template’> <Setter.Value> <ControlTemplate TargetType=’ContentControl’> <Grid> <Ellipse Width='{TemplateBinding Width}’ Height='{TemplateBinding Width}’ Fill='{TemplateBinding Background}’/> <ContentPresenter VerticalAlignment=’Center’ HorizontalAlignment=’Center’/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> … <ContentControl Width=’75’ Height=’100′ Style='{StaticResource ContentCtrl}’ Content=’Hello’/>
How can I add a CommandBinding to a window using markup?
CommandBinding can be added to a window using markup. Note that in XAML, the CommandBindingCollection is not declared in the markup as an element and the collection object is inferred by the type that the property takes and you populate the property element with one or more CommandBinding elements. [XAML] <Window.CommandBindings> <CommandBinding Command=’ApplicationCommands.Open’ Executed=’OpenCmdExecuted’ CanExecute=’OpenCmdCanExecute’/> </Window.CommandBindings>
How do I access extended properties from a XAML tempalte?
Here is how you would access a Canvas.Left extended property for example: <DataTemplate x:Key=’MyTemplate1′> <Path Data='{Binding Geometry}’> <Path.RenderTransform> <!– The Item property returns a visual hosted inside the Canvas –> <RotateTransform Angle=’60’ CenterX='{Binding Item.(Canvas.Left)}’ CenterY='{Binding Item.(Canvas.Top)}’ > </RotateTransform> </Path.RenderTransform> </Path> </DataTemplate>
How can I create a Style deriving from another Style
Perhaps you want your two ‘TextBlock’ elements to share some property values such as the FontFamily and the centered HorizontalAlignment, but you also want the text ‘My Pictures’ to have some additional properties. You can do that by creating a new style that is based on the first style, as shown here. [XAML] <Window.Resources> … <!–A Style that extends the previous MyBaseStyle Style–> <!–This is a ‘named style’ with an x:Key of MyDerivedStyle–> <Style BasedOn='{StaticResource {x:Type MyBaseStyle}}’ TargetType=’TextBlock’ x:Key=’MyDerivedStyle’> <Setter Property=’FontSize’ Value=’26’/> <Setter Property=’Foreground’> <Setter.Value> <LinearGradientBrush StartPoint=’0.5,0′ EndPoint=’0.5,1′> <LinearGradientBrush.GradientStops> <GradientStop Offset=’0.0′ Color=’#90DDDD’ /> <GradientStop Offset=’1.0′ Color=’#5BFFFF’ /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </Setter.Value> </Setter> </Style>
How can I set an attached property in code?
The following example shows how you can set an attached property in code. [C#] DockPanel myDockPanel = new DockPanel(); CheckBox myCheckBox = new CheckBox(); myCheckBox.Content = ‘Hello’; myDockPanel.Children.Add(myCheckBox); DockPanel.SetDock(myCheckBox, Dock.Top);