Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WPF, is there a way to bind Axis label

Solved!
Go to solution

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>
0 Kudos
Message 1 of 11
(5,173 Views)

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.

~ Paul H
0 Kudos
Message 2 of 11
(5,139 Views)

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>

 

 

0 Kudos
Message 3 of 11
(5,136 Views)
Solution
Accepted by topic author kirko7

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>

~ Paul H
0 Kudos
Message 4 of 11
(5,126 Views)

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>

 

0 Kudos
Message 5 of 11
(5,122 Views)
Solution
Accepted by topic author kirko7

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.

~ Paul H
0 Kudos
Message 6 of 11
(5,097 Views)

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!

0 Kudos
Message 7 of 11
(5,077 Views)

No worries.  Glad to help 🙂

~ Paul H
0 Kudos
Message 8 of 11
(5,072 Views)

Hi,

Is there any way to set font color withour re-seting the entire label template?

Thanks

0 Kudos
Message 9 of 11
(4,451 Views)

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.)

~ Paul H
0 Kudos
Message 10 of 11
(4,442 Views)