Create/generate style from properties automatically - wpf

I have a XAML Markup with multiple lines.
<telerik:RadTileView x:Name="RadTileView"
HorizontalAlignment="Center"
Height="480"
Margin="40"
ColumnWidth="160"
RowHeight="160"
ColumnsCount="4"
RowsCount="3"
MaxRows="3"
IsSelectionEnabled="True"
MaximizeMode="Zero"
SelectionMode="Single"
ItemsSource="{Binding Path=MenuItems}"
SelectedItem="{Binding Path=SelectedItem}"
ContentTemplate="{DynamicResource TileItemDataTemplate}">
Now I am done developing I want to simplify my markup and put the properties into a style.
But then I have to create a new Style and copy and paste each and ever property into it.
Is there an extension or addon to do the work for me?

Blend has (or at least did in Expression Blend) this functionality.
Extract styles in Expression Blend

Related

SciChart: Custom RolloverModifierLabel for multiple DataSeries bound from code

Before I updated to the latest version of SciChart, I had this custom rollover modifier that displayed multiple values for any given point I "rolled over".
It was implemented like this:
<sci:RolloverModifier
DrawVerticalLine="True"
ShowTooltipOn="Always"
SourceMode="AllVisibleSeries"
TooltipLabelTemplate="{StaticResource RolloverLabelTemplate}" />
RolloverLabelTemplate was a ControlTemplate:
<ControlTemplate
x:Key="RolloverLabelTemplate"
TargetType="sci:TemplatableControl">
<Grid>
...
Now the RolloverModifier.TooltipLabelTemplate is gone from the API and seems to be replaced by TooltipTemplate, which takes a DataTemplate, not a ControlTemplate. I tried making an analogous DataTemplate:
<DataTemplate
x:Key="SomeTemplate"
DataType="s:XySeriesInfo">
<Grid>
But when I try to assign it to the RolloverModifier,
<s:RolloverModifier
...
TooltipTemplate="{StaticResource SomeTemplate}" />
I get the following exception:
Unable to cast object of type
'SciChart.Charting.ChartModifiers.RolloverModifier' to type
'SciChart.Charting.Visuals.RenderableSeries.BaseRenderableSeries'.
I've tried to follow this documentation: https://www.scichart.com/documentation/v4.x/webframe.html#RolloverModifier.html
and on the topic of styling the tooltip template, it suggests to have a RolloverModifier, but to add the TooltipTemplate to the RenderableSeries:
<s:SciChartSurface.RenderableSeries>
<s:FastLineRenderableSeries s:RolloverModifier.TooltipTemplate="{StaticResource XyTooltipTemplate}"/>
</s:SciChartSurface.RenderableSeries>
<s:SciChartSurface.ChartModifier>
<s:ModifierGroup>
<s:RolloverModifier ShowTooltipOn="Always" />
</s:ModifierGroup>
</s:SciChartSurface.ChartModifier>
</s:SciChartSurface>
This is a problem for me, because I don't have the RenderableSeries defined in xaml. They are bound to the view model:
<sciVisuals:SciChartSurface
...
SeriesSource="{Binding SciSeries}">
there will be more than one, and in fact I don't even know how many. How can I customize the rollover tooltip label in this case?
In SciChart WPF v4.x or later, it is necessary to apply the TooltipTemplate to the series, because many users requested the ability to individually style series. For example a Candlestick series should have a different tooltip than a Line series.
Please see the SciChart WPF Documentation for applying a RolloverModifier TooltipTemplate in v4+:
Styling the Tooltip Item Template
SciChart by default has a number of Tooltip templates which are unique to the series type.
To change the Tooltip template, use the RolloverModifier.TooltipTemplate attached property:
<s:SciChartSurface >
<s:SciChartSurface.Resources>
<!-- Tooltip Template for an XyDataSeries binds to XySeriesInfo -->
<!-- Check out the properties on XySeriesInfo to see what you can bind to -->
<DataTemplate x:Key="XyTooltipTemplate" DataType="s:XySeriesInfo">
<StackPanel Orientation="Vertical">
<TextBlock Foreground="White">
<Run Text="Series: "/>
<Run Text="{Binding SeriesName, StringFormat='{}{0}:'}"/>
</TextBlock>
<TextBlock Foreground="White">
<Run Text="X-Value: "/>
<Run Text="{Binding FormattedXValue}"/>
</TextBlock>
<TextBlock Foreground="White">
<Run Text="Y-Value: "/>
<Run Text="{Binding FormattedYValue}"/>
</TextBlock>
</StackPanel>
</DataTemplate>
</s:SciChartSurface.Resources>
<s:SciChartSurface.RenderableSeries>
<s:FastLineRenderableSeries s:RolloverModifier.TooltipTemplate="{StaticResource XyTooltipTemplate}"/>
</s:SciChartSurface.RenderableSeries>
<s:SciChartSurface.ChartModifier>
<s:ModifierGroup>
<s:RolloverModifier ShowTooltipOn="Always" />
</s:ModifierGroup>
</s:SciChartSurface.ChartModifier>
</s:SciChartSurface>
What I suggest in order to style these despite using the MVVM API is to create your styles in XAML and apply them using the SeriesBinding MVVM API.
There is an FAQ in the SciChart WPF Documentation on how to style a tooltip when using the MVVM API here.
A lot of customers ask us how to style various attached properties used in SciChart in the MVVM API. For example, the given following attached properties, how do we convert this to the MVVM API?
The solution is simple, to use our method of Styling the RenderableSeries presented in Worked Example - Style a Series in MVVM.
Simply declare the attached properties and tooltip templates in a style in your View.
Now, apply the style using BaseRenderableSeriesViewModel.StyleKey property. SciChart will pick up the style and do the rest.
If you are still using the older, SeriesSource API (note this is now obsolete as of v4.x) then I suggest declare your style in XAML and apply it using this technique to access a style in code-behind.

Place a ContentControl inside a custom control XAML

I created a custom control and what to use a ContentControl inside that control for use of the MVVM design pattern however my control does not like this when I run the application. For testing I also tried other standard controls but none of them work inside the custom control... just more custom controls that are dependant on the parent custom control.
Does anybody suggest how to place standard controls such as the ContentControl inside a custom control?
Cheers.
EDIT
XamlParseException -> 'Add value to collection of type
'System.Collections.ObjectModel.ObservableCollection(Ribbon_Framework.RibbonTabItem)'
threw an exception.' Line number '8' and line position '14'.
<Ribbon:Ribbon AutomaticStateManagement="True" x:Name="Ribbon">
<ContentControl x:Name="SearchRibbon" Content="{Binding Path=SearchRibbon}" ContentTemplate="{DynamicResource SearchRibbonTemplate}" />
</Ribbon:Ribbon>
Inside the contentcontrol ->
<DataTemplate x:Key="SearchRibbonTemplate">
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Ribbon:RibbonTabItem Header="Search">
<Ribbon:RibbonGroupBox Header="{Binding Path=DisplayName}" Width="100">
<Ribbon:Button Width="100" Icon="{Binding Path=TemplateResource}" LargeIcon="{Binding Path=TemplateResource}" Command="{Binding Path=Commands}" />
</Ribbon:RibbonGroupBox>
</Ribbon:RibbonTabItem>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
Your Ribbon control expects an object of RibbonTabItem type (since it houses an
ObservableCollection<RibbonTabItem>
so you can only add the RibbonTabItem class to it - you need to make sure your control allows other elements inside it. Some 3rd party controls get round this by providing a content control inside the inner item of their custom control (i.e. let RibbonTabItem have a ContentControl inside it) or allow you to customise the item template
You need to change your implementation of Ribbon or change the functionality of RibbonTabItem for this to work. Look at ItemsControl.Items property and see what type that is. You should try using that type for your ObservableCollection

Dynamic Binding for Settings?

I am facing a problem, I have a application with several setting's files but which have exactly the same fields.
Sample :
Profil1.settings
Profil2.settings
Profil3.settings
What I'd like to do in my setting window XAML is to dynamicaly change the binding source for each profile.
At the moment my XAML binding look like this :
SelectedValue="{Binding Source={x:Static Local:Properties.Profil1.Default}, Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
My problem is how to replace the "Profil1" by "Profil2" without remaking all my Binding one by one for each controls via my code behind ? Is that possible to use some king of reflection variable in the XAML binding source and than simply change the type of that variable with Profil1 type or Profil2 type ?
Anyone can help me about this ?
Thank in advance.
What you want to do is set the DataContext of your settings window to the appropriate profile. You can do it anyway you want, but by doing so, all your bindings would point to that object. To make the bindings work like you have now, you'd do:
<Window x:Class="MyNamespace.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="clr-namespace:MyNamespace"
DataContext="{Binding Source={x:Static Local:Properties.Profil1.Default}}">
...
<ComboBox SelectedValue="{Binding Path=CurrentProfil, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />

WPF - What to do when a Dependency Property is another Control in the XAML

I'm hoping this will be my last question today. I'm in a hurry and google is not helping much (that or I'm searching the wrong places).
I created some custom properties and behaviors so my RadioButtons can alter my labels Content and the mask of my TextBoxes.
I could pass a String as a property, but how do I pass another control as a property? (AdjustedLabel is of type Label)
<RadioButton i:CPF_CNPJAdjustBehavior.LabelContent="Apple" i:CPF_CNPJAdjustBehavior.AdjustedLabel="??????????" Content="CPF" Height="16" HorizontalAlignment="Left" Margin="30,216,0,0" Name="radioButton1" VerticalAlignment="Top" GroupName="a" IsChecked="True">
<int:Interaction.Behaviors>
<i:CPF_CNPJAdjustBehavior/>
</int:Interaction.Behaviors>
</RadioButton>
<Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="20,81,0,0" Name="MyLabel" VerticalAlignment="Top" />
What do I have do write in "?????????" to set AdjustedLabel to the label named "MyLabel" ?
Thanks in Advance
Clark
AdjustedLabel="{Binding ElementName=MyLabel}" will do what you're searching for.
Consider reading some documentation for getting started with WPF, Bindings, Dependency Properties and XAML syntax. You could start with XAML Syntax In Detail, Dependency Properties Overview and Data Binding Overview.

Is it Possible to apply a DataTemplate to a Page?

I'm trying to follow the MVVM pattern laid out here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090097 I have this in my MainWindowResources.xaml file:
<DataTemplate DataType="{x:Type vm:VendorsViewModel}">
<vw:Vendors/> <--- I get a "Can't put a page in a style" error in blend with this
</DataTemplate>
and I've got this in my MainWindow.xaml file
<Window.Resources>
<ResourceDictionary Source="MainWindowResources.xaml"/>
</Window.Resources>
The mainWindow.xaml file contains a menu on the left and page holder on the right. Can I apply a dataTemplate to a <Page>? Or does it have to be a <UserControl>? As it stands, nothing is being data bound, here's what I have on the page that I want to have the viewmodel applied to:
<Custom:DataGrid Margin="0,30,0,0" d:LayoutOverrides="Width" ItemsSource="{Binding Path=AllVendors, Mode=Default}" >
<Custom:DataGrid.Columns>
<Custom:DataGridTextColumn Header="Company Name" Binding="{Binding Path=Name}" />
</Custom:DataGrid.Columns>
</Custom:DataGrid>
DataTemplates are applied to Content, which in most cases is either the Content property of a ContentControl or the Items/ItemsSource property of an ItemsControl. Page is not derived from ContentControl (UserControl is) so a DataTemplate can't be applied to its Content.
From what you're doing here it doesn't sound like that's what you're trying to do though. It looks like you're trying to use a Page in a DataTemplate which is what the error is telling you. Page is treated like Window in that it is a root container that is intended to have visual Content defined in a xaml file. UserControl has a similar purpose but can be inserted anywhere into a layout. If you change vw:Vendors to be a UserControl that should get rid of this specific error but you should also consider whether you're gaining anything from having the UserControl instead of just putting its content directly into the DataTemplate - this can help discourage code-behind and force you to use your ViewModel correctly.

Resources