I'm currently working on an interpolation project. I need the user to enter the interpolation boundaries (basically, 2 double values) in one TextBox separated by some separator.
In my MainWindow.xaml.cs I have created a class ViewData whose fields are controls in the user interface. And assigned my DataContext to it. Like this:
public partial class MainWindow : Window
{
ViewData viewData = new();
public MainWindow()
{
InitializeComponent();
DataContext = viewData;
}
}
In particular, this class has two fields of type double: boundA and boundB. I'd like to be able to take users input from TextBox and bind first value to boundA, second one to boundB. My ViewData class:
using System;
using System.Collections.Generic;
using System.Windows;
using CLS_lib;
namespace Splines
{
public class ViewData
{
/* RawData Binding */
public double boundA {get; set;}
public double boundB {get; set;}
public int nodeQnt {get; set;}
public bool uniform {get; set;}
public List<FRaw> listFRaw { get; set; }
public FRaw fRaw { get; set; }
public RawData? rawData {get; set;}
public SplineData? splineData {get; set;}
/* --------------- */
/* SplineData Binding */
public int nGrid {get; set;}
public double leftDer {get; set;}
public double rightDer {get; set;}
/* ------------------ */
public ViewData() {
boundA = 0;
boundB = 10;
nodeQnt = 15;
uniform = false;
nGrid = 20;
leftDer = 0;
rightDer = 0;
listFRaw = new List<FRaw>
{
RawData.Linear,
RawData.RandomInit,
RawData.Polynomial3
};
fRaw = listFRaw[0];
}
public void ExecuteSplines() {
try {
rawData = new RawData(boundA, boundB, nodeQnt, uniform, fRaw);
splineData = new SplineData(rawData, nGrid, leftDer, rightDer);
splineData.DoSplines();
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}
public override string ToString()
{
return $"leftEnd = {boundA}\n" +
$"nRawNodes = {nodeQnt}\n" +
$"fRaw = {fRaw.Method.Name}" +
$"\n";
}
}
}
UPDATE I've tried using IMultiValueConverter + MultiBinding but I failed at making it work :( Here's my MainWindow.xaml:
<userControls:ClearableTextBox x:Name="bounds_tb" Width="250" Height="40" Placeholder="Nodes amount">
<userControls:ClearableTextBox.Text>
<MultiBinding Converter="{StaticResource boundConverter_key}" UpdateSourceTrigger="LostFocus" Mode="OneWayToSource">
<Binding Path="boundA"/>
<Binding Path="boundB"/>
</MultiBinding>
</userControls:ClearableTextBox.Text></userControls:ClearableTextBox>
And my Converter:
public class BoundariesMultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string boundaries;
boundaries = values[0] + ";" + values[1];
return boundaries;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
string[] splitValues = ((string)value).Split(';');
return splitValues;
}
}