Click here to Skip to main content
16,018,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have created a reusable slider control using Syncfusion. I read from a database to show the "set", "target" and other values. The reusable control as increment/decrement buttons, a checkbox, and a save button on it. I'm running into trouble when I place multiple sliders. I have created a case statement to load the proper values for each slider, however, I don't know how I can determine which control's button has been clicked so I can increment/decrement/change checkbox/etc. of just that slider control that has been click.

Thank you in advance for taking the time to review my problem!

What I have tried:

Below is the generic slider user control:

Razor
<usercontrol x:class=" height="Auto" width="310">



Below is the placement of two controls onto a grid:
Razor
 <stackpanel x:name="ExercisePositionSetter" margin="1255,3,0,0" height="2017" background="Transparent" panel.zindex="0">
            <grid x:name="ExercisePositonSetterGrid">
                <grid.columndefinitions>
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="160">
                    <columndefinition width="*">

                

                <grid.rowdefinitions>
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="125">
                    <rowdefinition height="135">

                
                <textblock grid.column="0" grid.row="0" grid.columnspan="2" horizontalalignment="Center" verticalalignment="Center" 
="" foreground="{StaticResource KennetixDarkGreen}" fontsize="50" fontweight="SemiBold" textalignment="Center" text="Lever Height">
                
                <textblock grid.column="3" grid.row="0" grid.columnspan="2" horizontalalignment="Center" verticalalignment="Center" 
="" foreground="{StaticResource KennetixDarkGreen}" fontsize="50" fontweight="SemiBold" textalignment="Center" text="Lever Length">
                

<controls:motorsliderverticalcontrol 
="" x:name="LeftHeight" horizontalalignment="Center" verticalalignment="top" grid.column="0" grid.row="1" grid.columnspan="2" grid.rowspan="10" tag="Lever Height - Left">

                <controls:motorsliderverticalcontrol 
="" x:name="LeftLeverLength" horizontalalignment="Center" verticalalignment="top" grid.column="3" grid.row="1" grid.columnspan="2" grid.rowspan="10" tag="Lever Length - Left">


Below are the methods for updating the view when the buttons click. I believe there is a way to know which control is being clicked on so I could update the control's values:
C#
 public void ShowInitialSetPositions()
        {
            ObservableCollection<motorviewmodel> tempMotorViewModels = new ObservableCollection<motorviewmodel>();
            
            foreach (MotorViewModel motorVM in motorViewModels)
            {
                switch (motorVM.name)
                {
                    case "Lever Height - Left":
                        tempMotorViewModels.Add(new MotorViewModel(motorVM.driver) { currentPos = motorVM.currentPos, SetPositon = SetLeverHeightLeft, name = motorVM.name, PresetPosition = SetLeverHeightLeft, TargetPosition = motorVM.TargetPosition, motorAssemblyDefaultParameters = motorVM.motorAssemblyDefaultParameters });
                        //newmotorViewModel.Add(new MotorViewModel(driver) { currentPos = motorVM.currentPos, SetPositon = motorVM.SetPositon + SetLeverHeightLeft, name = motorVM.name, PresetPosition = motorVM.PresetPosition, TargetPosition = motorVM.TargetPosition, motorAssemblyDefaultParameters = motorVM.motorAssemblyDefaultParameters });
                        //motorViewModels.Add(tempMotorViewModels);
                        break;

                    case "Lever Length - Left":
                        tempMotorViewModels.Add(new MotorViewModel(motorVM.driver) { currentPos = motorVM.currentPos, SetPositon = SetLeverLengthLeft, name = motorVM.name, PresetPosition = SetLeverLengthLeft, TargetPosition = motorVM.TargetPosition, motorAssemblyDefaultParameters = motorVM.motorAssemblyDefaultParameters });
                       // motorViewModels = tempMotorViewModels;
                        break;
                }
            }
            motorViewModels = tempMotorViewModels;
        }


        public void AdjustPresetPositions(int delta)
        {
            ObservableCollection<motorviewmodel> tempMotorViewModels = new ObservableCollection<motorviewmodel>();
            foreach (MotorViewModel motorVM in motorViewModels)
            {
                switch (motorVM.name)
                {
                    case "Lever Height - Left":
                        tempMotorViewModels.Add(new MotorViewModel(motorVM.driver) { currentPos = motorVM.currentPos, SetPositon = SetLeverHeightLeft, name = motorVM.name, PresetPosition = motorVM.PresetPosition += delta, TargetPosition = motorVM.TargetPosition, motorAssemblyDefaultParameters = motorVM.motorAssemblyDefaultParameters });
                        //newmotorViewModel.Add(new MotorViewModel(driver) { currentPos = motorVM.currentPos, SetPositon = motorVM.SetPositon + SetLeverHeightLeft, name = motorVM.name, PresetPosition = motorVM.PresetPosition, TargetPosition = motorVM.TargetPosition, motorAssemblyDefaultParameters = motorVM.motorAssemblyDefaultParameters });
                        break;

                    case "Lever Length - Left":
                        tempMotorViewModels.Add(new MotorViewModel(motorVM.driver) { currentPos = motorVM.currentPos, SetPositon = SetLeverLengthLeft, name = motorVM.name, PresetPosition = motorVM.PresetPosition += delta, TargetPosition = motorVM.TargetPosition, motorAssemblyDefaultParameters = motorVM.motorAssemblyDefaultParameters });
                        break;
                }
            }
            motorViewModels = tempMotorViewModels;
        }


Below are the converters for the Syncfusion slider that is used to set the values:

namespace Converters
{
    public class CurrentPositionConverter : IValueConverter
    {
        public UIElement? UIParameter { get; set; }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var rootElement = UIParameter as Border;
            if (parameter != null)
            {
                var border = rootElement?.FindName(parameter.ToString());
                var userControl = (border as Border).Parent;
                var tag = (userControl as MotorSliderVerticalControl).Tag;
                foreach (var item in value as ObservableCollection<motorviewmodel>)
                {
                    if (item.name == tag.ToString())
                    {
                        return item.currentPos;
                    }
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

    public class SetPositionConverter : IValueConverter
    {
        public UIElement? UIParameter { get; set; }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var rootElement = UIParameter as Border;
            if (parameter != null)
            {
                var border = rootElement?.FindName(parameter.ToString());
                var userControl = (border as Border).Parent;
                var tag = (userControl as MotorSliderVerticalControl).Tag;
                foreach (var item in value as ObservableCollection<motorviewmodel>)
                {
                    if (item.name == tag.ToString())
                    {
                        return item.SetPositon;
                    }
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

    public class TargetPositionConverter : IValueConverter
    {
        public UIElement? UIParameter { get; set; }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var rootElement = UIParameter as Border;
            if (parameter != null)
            {
                var border = rootElement?.FindName(parameter.ToString());
                var userControl = (border as Border).Parent;
                var tag = (userControl as MotorSliderVerticalControl).Tag;
                foreach (var item in value as ObservableCollection<motorviewmodel>)
                {
                    if (item.name == tag.ToString())
                    {
                        return item.PresetPosition;
                    }
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

    public class MaxTravelConverter : IValueConverter
    {
        public UIElement? UIParameter { get; set; }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var rootElement = UIParameter as Border;
            if (parameter != null)
            {
                
                var border = rootElement?.FindName(parameter.ToString());
                var userControl = (border as Border).Parent;
                var tag = (userControl as MotorSliderVerticalControl).Tag;
                foreach (var item in value as ObservableCollection<motorviewmodel>)
                {
                    if (item.name == tag.ToString())
                    {
                        //return item.Item1.settings.maxTravel;
                        if (item.motorAssemblyDefaultParameters.ContainsKey("MotorMaxSteps"))
                        {
                            return item.motorAssemblyDefaultParameters["MotorMaxSteps"];
                        }
                    }
                }
            }

            return null;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
                
    }
Posted
Updated 3-Sep-22 23:17pm
v3
Comments
[no name] 2-Sep-22 12:16pm    
Typically, one uses the .Name or .Tag to identify a control; yours is a confusion. You've also created dependencies between controls that are pointless (grid rows size automatically based on content; one rarely "sizes" them "relative" to another control). There is also a host of pointless casting when everything is sharing a common base; i.e. "Control". Without a "rewrite", any suggestions just compound the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900