绑定到随组合框索引变化的数组元素
Binding to an array's element changing with combobox index
class DAC : INotifyPropertyChanged
{
private uint _value;
private bool _isCurrent;
private string _name;
private uint _address;
private bool _powerDown;
private bool _lowPower;
private bool _enable;
public bool Enable
{
get { return _enable; }
set
{
if (_enable == value)
{
return;
}
_enable = value;
OnPropertyChanged("Enable");
}
}
//The rest of the property definitions looks like the first one
public uint Value
{
get { return _value; }
set
{
if (_value == value)
{
return;
}
_value = value;
OnPropertyChanged("Value");
}
}
#region INotifyProperty Definitions
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
所以这是我的基本 class,我打算用它来填充列表。我基本上有一个列在组合框中的 DAC 数组(或 DAC 的其他衍生产品)。
组合框用
填充自身
combobox1.SetBinding(ComboBox.ItemsSourceProperty, new Binding() { Source = cdacList });
combobox1.DisplayMemberPath = "Name";
当用户选择其中一项时,应该根据从组合框中选择的索引更新文本框和滑块的值,并使用该索引访问数组中的数据。
我试过直接绑定
textbox1.SetBinding(TextBox.TextProperty, new Binding("Address") { Source = cdacList[combobox1.SelectedIndex] });
slider1.SetBinding(Slider.ValueProperty, new Binding("Value") { Source = cdacList[combobox1.SelectedIndex] });
但它不会自行更新。我知道我需要一些其他方面来通过绑定来做到这一点,但我自己想不通。
您应该能够如下所示设置一次绑定,以后无需更改它们:
textbox1.SetBinding(TextBox.TextProperty,
new Binding("SelectedItem.Address") { Source = combobox1 });
slider1.SetBinding(Slider.ValueProperty,
new Binding("SelectedItem.Value") { Source = combobox1 });
也就是说,应该在 XAML:
中创建绑定
<TextBox x:Name="textbox1"
Text="{Binding SelectedItem.Address, ElementName=combobox1}" .../>
<Sliderx:Name="slider1"
Text="{Binding SelectedItem.Value, ElementName=combobox1}" .../>
class DAC : INotifyPropertyChanged
{
private uint _value;
private bool _isCurrent;
private string _name;
private uint _address;
private bool _powerDown;
private bool _lowPower;
private bool _enable;
public bool Enable
{
get { return _enable; }
set
{
if (_enable == value)
{
return;
}
_enable = value;
OnPropertyChanged("Enable");
}
}
//The rest of the property definitions looks like the first one
public uint Value
{
get { return _value; }
set
{
if (_value == value)
{
return;
}
_value = value;
OnPropertyChanged("Value");
}
}
#region INotifyProperty Definitions
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
所以这是我的基本 class,我打算用它来填充列表。我基本上有一个列在组合框中的 DAC 数组(或 DAC 的其他衍生产品)。 组合框用
填充自身combobox1.SetBinding(ComboBox.ItemsSourceProperty, new Binding() { Source = cdacList });
combobox1.DisplayMemberPath = "Name";
当用户选择其中一项时,应该根据从组合框中选择的索引更新文本框和滑块的值,并使用该索引访问数组中的数据。
我试过直接绑定
textbox1.SetBinding(TextBox.TextProperty, new Binding("Address") { Source = cdacList[combobox1.SelectedIndex] });
slider1.SetBinding(Slider.ValueProperty, new Binding("Value") { Source = cdacList[combobox1.SelectedIndex] });
但它不会自行更新。我知道我需要一些其他方面来通过绑定来做到这一点,但我自己想不通。
您应该能够如下所示设置一次绑定,以后无需更改它们:
textbox1.SetBinding(TextBox.TextProperty,
new Binding("SelectedItem.Address") { Source = combobox1 });
slider1.SetBinding(Slider.ValueProperty,
new Binding("SelectedItem.Value") { Source = combobox1 });
也就是说,应该在 XAML:
中创建绑定<TextBox x:Name="textbox1"
Text="{Binding SelectedItem.Address, ElementName=combobox1}" .../>
<Sliderx:Name="slider1"
Text="{Binding SelectedItem.Value, ElementName=combobox1}" .../>