07-07-2015 04:37 PM
The following problem is very similar to this thread, but I'm looking for a solution that follows the MVVM pattern and doesn't change UI elements in the code-behind.
Scenario: I have a CheckBox element that, when checked, will change the value of a boolean called LogarithmicXAxis in the ViewModel. I want my Graph's x-axis ScaleKind property to respond by toggling between Linear and LogarithmBase2. So I wrote an IValueConverter class as follows:
class BooleanScalekindConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if ((bool)value) return NationalInstruments.Controls.RangeScaleKind.LogarithmBase2; else return NationalInstruments.Controls.RangeScaleKind.Linear; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value.Equals(NationalInstruments.Controls.RangeScaleKind.LogarithmBase2)) return true; else return false; } }
Then I bound the ScaleKind property to LogarithmicXAxis:
<ni:AxisDouble x:Name="xAxis" Orientation="Horizontal" Range="-10, 10, System.Double" ScaleKind="{Binding LogarithmicXAxis, Converter={StaticResource BooleanScalekindConverter}, UpdateSourceTrigger=PropertyChanged}" />
When I try to debug, I can see that the LogarithmicXAxis value is updating correctly (and a different part of my app responds to a change in that value). However, there must be something wrong with the way I'm binding, or possibly with my converter, because the converter methods never get called.
I am relatively new to WPF, Measurement Studio, and MVVM, so any suggestions would be greatly appreciated!
Solved! Go to Solution.
07-08-2015 05:39 PM
khawesdo,
What version of Measurement Studio and Visual Studio are you using?
07-08-2015 05:45 PM
Measurement Studio 2013 (v. 13.0.0.242)
Visual Studio Ultimate 2012
07-09-2015 06:57 PM
Hi, khawesdo
I think your binding is almost there. You should do this (I've pasted the entire XAML for context, making the relevant parts bold, pardon my nasty namespace name ):
<Window x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ni="http://schemas.ni.com/controls/2009/xaml/presentation" xmlns:local="clr-namespace:WpfApplication5" Title="MainWindow" Height="480" Width="640" > <Window.Resources> <local:BooleanScalekindConverter x:Key="BooleanToScaleKindConverter" /> </Window.Resources> <Grid> <ni:Graph HorizontalAlignment="Left" Height="200" Margin="0" VerticalAlignment="Top" Width="300"> <ni:Graph.Axes> <ni:AxisDouble x:Name="xAxis" Orientation="Horizontal" Range="-10, 10, System.Double" ScaleKind="{Binding Value, Converter={StaticResource BooleanToScaleKindConverter}, ElementName=LogarithmicXAxis, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </ni:Graph.Axes> </ni:Graph> <ni:Switch x:Name="LogarithmicXAxis" Content="Switch" FalseContent="Off" HorizontalAlignment="Left" Height="23" TrueContent="On" VerticalAlignment="Top" Width="100" Margin="22,231,0,0" Value="False"/> </Grid> </Window>
Here, I am binding specifically to the LogarithmicXAxis
element's Value property.
I hope that helps!