I Hate Java I Hate Java - 3 years ago 149
C# Question

Bind DataTemplate to a unique variable

I'm trying to create multiple sliders but each slider only changes the last object to be created

XML

<DataTemplate x:Key = "processTemplate">
<StackPanel Orientation = "Horizontal">
<TextBlock Text = "{Binding Path = Name, Mode = TwoWay}" Width = "100" Margin = "3 5 3 5"/>
<TextBlock Text = "{Binding Path = ID, Mode = TwoWay}" Width = "100" Margin = "0 5 3 5"/>
<Slider Value="{Binding Path = Volume, Mode = TwoWay}" Maximum="100" Width="100"/>
</StackPanel>
</DataTemplate>


CLASS

public class ProcessList
{
private string name;
public string Name { get; set; }

private int id;
public int ID { get; set; }

private float volume;
public float Volume {
get { return volume; }
set {
if (volume != value) {
volume = value;
SetApplicationVolume(this.id, volume);
MessageBox.Show(this.id.ToString());
}
}
}

}


and how it's populated

processes.Add(new ProcessList() { Name = theprocess.ProcessName, ID = theprocess.Id, Volume = VolumeMixer.GetApplicationVolume(theprocess.Id)});


How can I change it so each slider changes the volume of its process

If anyone is as stupid as me here is the corrected code:

private string name = "";
public string Name {
get {
return name;
}
set {
if (name != value) {
name = value;
NotifyPropertyChanged("Name");
}
}
}

private int id = 0;
public int ID {
get {
return id;
}
set {
if (id != value) {
id = value;
NotifyPropertyChanged("ID");
}
}
}

Answer Source

The ID property is not synchronized with its backing field id, and id is always the same 0.

Also, you should pass ID and Volume through the constructor, because your code rely on the assumption that one would always assign Volume after the ID.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download