08-13-2015 09:49 AM
I have this XAML code and I was wondering if there's a way to programatically change the Axis label from GHz to something else. I tried the usual binding and it didn't work.
Is it just easier to use a separate TextBox control over the same spot?
Thanks!
<ni:AxisDouble Orientation="Horizontal">
<ni:AxisDouble.LabelTemplate>
<DataTemplate>
<Label Content="GHz">
</DataTemplate>
</ni:AxisDouble.LabelTemplate>
<ni:AxisDouble.MajorGridLines>
<ni:GridLines StrokeThickness="0.3"/>
</ni:AxisDouble.MajorGridLines>
</ni:AxisDouble>
Solved! Go to Solution.
08-13-2015 01:53 PM
The data context for the LabelTemplate is the Label on the axis. You should be able to set Label programmatically or through a binding, and use Content="{Binding}" in your LabelTemplate.
08-13-2015 01:59 PM
I already tried this but it doesn't work. Am I missing something?
<ni:AxisDouble Orientation="Horizontal">
<ni:AxisDouble.LabelTemplate>
<DataTemplate>
<Label Content="{Binding LabelX}">
</DataTemplate>
</ni:AxisDouble.LabelTemplate>
<ni:AxisDouble.MajorGridLines>
<ni:GridLines StrokeThickness="0.3"/>
</ni:AxisDouble.MajorGridLines>
</ni:AxisDouble>
08-13-2015 02:04 PM
Using Content="{Binding LabelX}" in the LabelTemplate will only work if Label is set to an object with a property called LabelX. I believe what you want is something like this:
<ni:AxisDouble Orientation="Horizontal" Label="{Binding LabelX}">
<ni:AxisDouble.LabelTemplate>
<DataTemplate>
<Label Content="{Binding}" />
</DataTemplate>
</ni:AxisDouble.LabelTemplate>
</ni:AxisDouble>
08-13-2015 02:25 PM - edited 08-13-2015 02:26 PM
Interesting.... so now, using your example, "GHz" does show up when the form loads up but when I try to change this label by performing other action, it doesn't refresh.
If you put a break point on this line set { m_xAxisLabel = value; OnPropertyChanged("XAxisLabel"); } I can see that the new string makes it over but GUI doesn't update for some reason. I even included UpdateSourceTrigger setting.
private string m_xAxisLabel = "GHz";
public string XAxisLabel
{
get { return m_xAxisLabel; }
set { m_xAxisLabel = value; OnPropertyChanged("XAxisLabel"); }
}
<ni:AxisDouble Label="{Binding XAxisLabel, UpdateSourceTrigger=PropertyChanged}" Orientation="Horizontal" Adjuster="FitExactly">
<ni:AxisDouble.LabelTemplate>
<DataTemplate>
<Label Content="{Binding}"/>
</DataTemplate>
</ni:AxisDouble.LabelTemplate>
</ni:AxisDouble>
08-13-2015 02:45 PM
I was able to get the label binding to work using your setup. Here is the complete code I used:
XAML
<Grid>
<ni:Graph>
<ni:Graph.Axes>
<ni:AxisDouble Orientation="Horizontal" Adjuster="FitExactly"
Label="{Binding XAxisLabel, UpdateSourceTrigger=PropertyChanged}">
<ni:AxisDouble.LabelTemplate>
<DataTemplate>
<Label Content="{Binding}"/>
</DataTemplate>
</ni:AxisDouble.LabelTemplate>
</ni:AxisDouble>
</ni:Graph.Axes>
</ni:Graph>
<Button HorizontalAlignment="Right" VerticalAlignment="Top" Content="_Change Label" Click="OnButtonClicked" />
</Grid>
Code
public sealed class Model : INotifyPropertyChanged {
private string m_xAxisLabel = "GHz";
public string XAxisLabel {
get { return m_xAxisLabel; }
set { m_xAxisLabel = value; OnPropertyChanged( "XAxisLabel" ); }
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void OnPropertyChanged( string propertyName ) {
PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
}
}
public partial class MainWindow : Window {
private readonly Model model = new Model( );
public MainWindow( ) {
DataContext = model;
InitializeComponent( );
}
private void OnButtonClicked( object sender, RoutedEventArgs e ) {
model.XAxisLabel += "!";
}
}
Clicking on the button repeatedly updates the axis label. It also worked without the UpdateSourceTrigger.
08-13-2015 03:02 PM
My bad, I forgot NotifyPropertyChanged in my code (first time I used binding in this form).
My appologies for wasting your time debugging my issue.
Thank you!
08-13-2015 03:23 PM
No worries. Glad to help 🙂
11-16-2015 02:17 AM
Hi,
Is there any way to set font color withour re-seting the entire label template?
Thanks
11-16-2015 09:57 AM
The brush for division labels and ticks can be set through the LabelBrush and TickBrush properties, respectively. For a scale Label, the only way to modify the font color is to change the Foreground on the graph control (which will update the brushes on all child elements), or to supply a custom LabelTemplate as you mentioned.
(Also for future reference, you question is more likely to get an answer if you create a new post, rather than adding a comment to an existing question that is already marked as answered.)