【发布时间】:2009-06-18 14:05:41
【问题描述】:
在我看来,我有一个滑块和一个组合框。
当我更改滑块时,我希望组合框发生更改。
当我改变组合框时,我希望滑块改变。
我可以更新一个或另一个,但如果我尝试更新两个,我会收到 StackOverflow 错误,因为一个属性会在无限循环中不断更新另一个。
我尝试过在一个地方完成更新的 Recalculate(),但仍然遇到递归问题。
如何让每个控件在不进行递归的情况下更新另一个控件?
在视图中:
<ComboBox
ItemsSource="{Binding Customers}"
ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
Margin="20"
HorizontalAlignment="Left"
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/>
<Slider Minimum="0"
Maximum="{Binding HighestCustomerIndex, Mode=TwoWay}"
Value="{Binding SelectedCustomerIndex, Mode=TwoWay}"/>
在 ViewModel 中:
#region ViewModelProperty: SelectedCustomer
private Customer _selectedCustomer;
public Customer SelectedCustomer
{
get
{
return _selectedCustomer;
}
set
{
_selectedCustomer = value;
OnPropertyChanged("SelectedCustomer");
SelectedCustomerIndex = _customers.IndexOf(_selectedCustomer);
}
}
#endregion
#region ViewModelProperty: SelectedCustomerIndex
private int _selectedCustomerIndex;
public int SelectedCustomerIndex
{
get
{
return _selectedCustomerIndex;
}
set
{
_selectedCustomerIndex = value;
OnPropertyChanged("SelectedCustomerIndex");
SelectedCustomer = _customers[_selectedCustomerIndex];
}
}
#endregion
【问题讨论】:
标签: c# wpf mvvm data-binding