Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WPF Legend Binded to Graph.

Solved!
Go to solution

Hi,

 

I have a ni:Legend object binded to the Plots of my Graph object. Which Property of my Plot should I set from the C# code to see it displayed in the Legend window?

 

   <ni:Legend Name="MyLegend" ItemsSource="{Binding ElementName=MyGraph, Path=Plots}" />
           
 
        
        <ni:Graph Name="MyGraph" DataSource="{Binding UpdateSourceTrigger=PropertyChanged}">
            <ni:Graph.Plots>
                <ni:Plot/>
            </ni:Graph.Plots>
</ni:Graph>
MyGraph.Plots[0].??? = ???
0 Kudos
Message 1 of 5
(5,128 Views)

By default, the data template used for plots in the legend displays the Label property.

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

Thanks!

 

I am almost there now:

 

   var MyLabel1 = new Label();
            MyLabel1.Content = "Something";
            MyGraph.Plots[0].Label = MyLabel1;

 

But what I get in the Label window is. System.Window.Controls.Label: Something

 

How do I get rid of the unwanted information, and display only the Content of the Label?

 

 

 

0 Kudos
Message 3 of 5
(5,110 Views)
Solution
Accepted by topic author jtamaska

The legend uses a WPF ContentPresenter to display the plot Label property. In the case of UI elements, it appears that ContentPresenter defaults to using the ToString representation, instead of the element itself.


To display content like "Something", you will want to assign that content directly to the plot Label property:


    MyGraph.Plots[0].Label = "Something";


If you want to specifically show a WPF Label content control in the legend, you can use the LabelTemplate property:


    <ni:Graph.Plots>
        <ni:Plot>
            <ni:Plot.LabelTemplate>
                <DataTemplate>
                    <Label Content="{Binding}" />
                </DataTemplate>
            </ni:Plot.LabelTemplate>
        </ni:Plot>
    </ni:Graph.Plots>

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

That did the trick! Thank you!

0 Kudos
Message 5 of 5
(5,071 Views)