我想根据Visbility
的选定值设置Expander
的{{1}}。
ComboBox
已经映射到ComboBox
:
Model
我在VM中设置了一个以<ComboBox Name="SelectedCar" ItemsSource="{Binding Path=CarCategories}" SelectedValue="{Binding Path=Car.CarCategory, Mode=TwoWay}"/>
方式导出的属性:
Visbility
这就是我使用它的方式:
private Visibility _extraCarDetailsVisibility;
public Visibility ExtraCarDetailsVisibility
{
get
{
if (ManagedPortfolioSelected != null)
{
var category = Car.CarCategory.ToLower();
if (category == "porsche")
{
_extraCarDetailsVisibility= Visibility.Visible;
}
}
_extraCarDetailsVisibility= Visibility.Collapsed;
return _extraCarDetailsVisibility;
}
set
{
_extraCarDetailsVisibility= value;
NotifyPropertyChanged("ExtraCarDetailsVisibility");
}
}
然而,这不起作用,因为我认为<Expander Visibility="{Binding Path=ExtraCarDetailsVisibility}">
更改事件未订阅(而且似乎我不能真正来自模型)所以CarCategories
属性当我改变汽车类别时永远不会被召回......
你会怎么做?谢谢!
答案 0 :(得分:0)
那里有几个问题:
将一些事件挂钩到Car对象的PropertyChanged事件:
private ICar _carSelected; public IPortfolio CarSelected { get { return _carSelected; } set { if (_carSelected!= value) { if (_carSelected!= null) { _carSelected.PropertyChanged -= OnCarSelectedPropertyChanged; } _carSelected= value; // Also subscribing to any change that happens to the Carselected _carSelected.PropertyChanged += OnCarSelectedPropertyChanged; } // Notifying when the selected portfolio is changed NotifyPropertyChanged("CarSelected"); } }
CarSelected属性更改后触发事件:
void OnCarSelectedPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "CarCategory")
{
NotifyPropertyChanged("ExtraCarDetailsVisibility");
}
}
...那将更新我的可见性属性我已经连接到视图:
private Visibility _extraCarDetailsVisibility;
public Visibility ExtraCarDetailsVisibility
{
get
{
_extraCarDetailsVisibility= Visibility.Collapsed;
if (PendingCarSelected != null)
{
var category = CarSelected.CounterpartyCategory.ToLower();
if (category == "porsche" || category == "porsches")
{
_extraCarDetailsVisibility= Visibility.Visible;
}
}
return _extraCarDetailsVisibility;
}
set
{
_extraCarDetailsVisibility= value;
NotifyPropertyChanged("ExtraCarDetailsVisibility");
}
}