我无法使用以下代码。请注意TextBlock正在验证propertychanged事件是否按预期触发和更新。 TextBlock应该更新为False,但由于某种原因,本地:AccountListControl不会变为不可见。有什么想法吗?
的Xaml:
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>
<Grid>
<local:AccountListControl DataContext="{Binding AccountListVm}"
Visibility="{Binding AreAccountsVisible,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
<TextBlock Text="{Binding AreAccountsVisible}"/>
</Grid>
代码背后:
public class Page : Notifiable
{
CoaWorkspace _ws;
public Page(CoaWorkspace ws)
{
_ws = ws;
ws.Model.Stash.PropertyChanged += PropertyChangedHandler;
(ws.Model.Stash.Selected as ICoaPackage)
.PropertyChanged += PropertyChangedHandler;
}
public IAccountListVm AccountListVm
{
get { return _AccountListVm; }
protected set { SetField(ref _AccountListVm, value); }
}
private IAccountListVm _AccountListVm = null;
public bool AreAccountsVisible
{
get { return _ws.Model.Stash.Selected.Id > 0; }
}
private void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Selected" ||
e.PropertyName == "Id")
this.OnPropertyChanged("AreAccountsVisible");
}
}
更新
我添加了以下xaml并确认转换器和绑定工作正常。
<TextBlock Text="TEST TEXT" Visibility="{Binding AreAccountsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"/>
所以它必须与本地有关:AccountsListControl UserControl。我仍然有兴趣知道为什么这会导致问题。
答案 0 :(得分:3)
您的DataContext指向模型的错误级别。这是一个可能的解决方案
<Grid>
<Border Visibility="{Binding AreAccoutsVisible,
Converter={StaticResource BooleanToVisibilityConverter}}">
<local:AccountListControl DataContext="{Binding AccountListVm}"/>
</Border>
<TextBlock Text="{Binding AreAccountsVisible}"/>
</Grid>