How would go about setting the x and y axis titles. These are captions below the ticks (ie. Years)?
Thanks,
Steve
Here's an example of how you can make the axis titles more dynamic, through the use of attached properties:
The Xaml:
<Window.Resources> <ControlTemplate x:Key="chartTemplate" TargetType="igChart:XamChart"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <ContentPresenter Name="PART_Default_Chart" Grid.Row="0" Grid.Column="1" /> <TextBlock Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" TextAlignment="Center" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:AxisInfo.YAxisTitle)}"> <TextBlock.LayoutTransform> <RotateTransform Angle="90" /> </TextBlock.LayoutTransform> </TextBlock> <TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:AxisInfo.XAxisTitle)}" TextAlignment="Center" /> </Grid> </ControlTemplate> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <igChart:XamChart x:Name="theChart" Template="{StaticResource chartTemplate}" local:AxisInfo.XAxisTitle="XAxis" local:AxisInfo.YAxisTitle="YAxis"> </igChart:XamChart> <Button x:Name="changeTitles" Content="Change Titles" Click="changeTitles_Click" Grid.Row="1"/> </Grid>
And the code behind:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void changeTitles_Click(object sender, RoutedEventArgs e) { AxisInfo.SetXAxisTitle(theChart, "Another X title"); AxisInfo.SetYAxisTitle(theChart, "Another Y title"); } } public static class AxisInfo { public static readonly DependencyProperty XAxisTitleProperty = DependencyProperty.RegisterAttached( "XAxisTitle", typeof(string), typeof(AxisInfo), new PropertyMetadata("X Axis")); public static string GetXAxisTitle( DependencyObject target) { return (string)target.GetValue(XAxisTitleProperty); } public static void SetXAxisTitle( DependencyObject target, string title) { target.SetValue(XAxisTitleProperty, title); } public static readonly DependencyProperty YAxisTitleProperty = DependencyProperty.RegisterAttached( "YAxisTitle", typeof(string), typeof(AxisInfo), new PropertyMetadata("Y Axis")); public static string GetYAxisTitle( DependencyObject target) { return (string)target.GetValue(YAxisTitleProperty); } public static void SetYAxisTitle( DependencyObject target, string title) { target.SetValue(YAxisTitleProperty, title); } }
When I change the theme, it seems to work. The axis labels aren't effected by the change, because they are just in the static template that we are setting. Changing the theme works by changing the control templates of the Infragistics controls. But you are setting a template manually that has a static text styling on the labels. You may have to make multiple versions of this template and swap them as the theme changes to make the axis labels style change appropriately along with the theme.
for 3. I see the caption in this example, do you?
-Graham
thanks, I got it working.
Three questions:1. At the moment the template somehow overwrites the theme of the XamGraph. How can change that?2. How can I change the text of the textblocks after loading in the template?3. Unfortunately the Caption of the XamGraph is not visible anymore. What do I have to do?
Thanks for helping out!
Gawain
Another interesting way of moving from a programatically created control tree to turning it into a ControlTemplate might be to:
Hope that helps!
Do you want to be able to define the control template in C# or just assign it in C#? If its the latter you can just define that control template in your xaml resources (maybe in App.xaml?). And then assign it using something like xamChart1.Template = Resources["chartTemplate"]; where chartTemplate is the key you assigned to the resource.
If you want to create the entire template in code, then you should either:
1) Use that xaml as a string in your application and use XamlReader to hydrate it. Then assign it to the template property of the chart.
2) Create the template programmatically using FrameworkElementFactory, then assign it.
The second option there is more complex. A control template must create its content many times, and each time the content must be a distinct instance, thus it is defined as a factory that produces a new instance of the content on demand. When you define one in xaml, the transformation from an "instance" of what you want to create to a factory that produces an instance of the described is done automatically, so things are a bit more complicated if you want to do it by hand. What is the reason you want to define this in C#?
how can I do this with C# code?