将DataTemplate绑定到唯一变量

时间:2017-09-30 12:12:22

标签: c# wpf

我正在尝试创建多个滑块,但每个滑块只会更改要创建的最后一个对象

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());
            }
        }
    }

}

以及它是如何填充的

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

如何更改它以使每个滑块改变其过程的音量

如果有人像我这样愚蠢,这里是更正后的代码:

    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");
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

ID属性与其后备字段id不同步,id始终相同0。

此外,您应该通过构造函数传递ID和Volume,因为您的代码依赖于假设一个人总是在ID之后分配Volume。