I am writing a program in which i want Dictionary<String, List<int>> nameColor= new Dictionary<String, List<int>>();
I have 32 buttons and every button has a name "Btn00" to "Btn32". I am changing color of buttons on click, and i want to have dictionary which will contain button name and list of colors. Then I will parse to json to have {"Btn00":[0,0,0],"Btn01":[255,0,255], etc.}
. I have sliders with which I change color on click:
List<int> btnColors = new List<int>();
public void changeColor(Button button)
{
byte rr = (byte)SeekR.Value;
byte gg = (byte)SeekG.Value;
byte bb = (byte)SeekB.Value;
Color cc = Color.FromRgb(rr, gg, bb); //Create object of Color class.
SolidColorBrush colorBrush = new SolidColorBrush(cc); //Creating object of SolidColorBruch class.
button.Background = colorBrush; //Setting background of a button.
btnColors.Add(rr);
btnColors.Add(gg);
btnColors.Add(bb);
And I call changeColor function here:
private void changeBtnColor(object sender, RoutedEventArgs e)
{
changeColor(sender as Button);
}
I have tried making
String btnName;
Dictionary<String, List<int>> nameColor= new Dictionary<String, List<int>>();
but I don't know how can i put button names as string in dictionary, and then parse it to JSON so I would have above notation.
EDIT:
I changed my code like this:
public void changeColor(Button button)
{
List<int> btnColors = new List<int>();
Dictionary<String, List<int>> nameColor= new Dictionary<String, List<int>>();
byte rr = (byte)SeekR.Value;
byte gg = (byte)SeekG.Value;
byte bb = (byte)SeekB.Value;
Color cc = Color.FromRgb(rr, gg, bb); //Create object of Color class.
SolidColorBrush colorBrush = new SolidColorBrush(cc); //Creating object of SolidColorBruch class.
button.Background = colorBrush; //Setting background of a button.
//ledIndBack.Background = colorBrush;
btnColors.Add(rr);
btnColors.Add(gg);
btnColors.Add(bb);
nameColor.Add(button.Name, btnColors);
string json = JsonConvert.SerializeObject(nameColor);
Console.WriteLine(json);
}
And I get json values like {"Btn00":[255,0,0]} {"Btn10":[0,0,255]} etc. So it is not in the same object, and I probably need to use loop and SerializeObject outside the loop and I tried but every time I get the same response. Also, these codes are in my xaml.cs file. Is that going to be a problem, or should I add Commands and use functions in my ViewModel folder -> BtnViewModel.cs. I also have to send my data to the server, and I don't know how smart is it to do everything in xaml.cs.