我遇到的问题是,当我更改TextBox
布尔值ObservableCollection
时,我的IsFoo
不会隐藏也不可见。我不知道为什么会这样。我想念什么吗?
模型用于ObservableCollection`
public class FooModel: Model
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsFoo { get; set; }
public string FooStr{ get; set; }
}
模型在ViewModel
和View
之间使用
public class SomeModel: Model
{
public Model()
{
FooModels= new ObservableCollection<FooModel>();
}
private ObservableCollection<FooModel> _fooModels;
public ObservableCollection<FooModel> FooModels
{
get => _fooModels;
set => SetProperty(ref _fooModels, value);
}
}
查看
<ItemsControl FontWeight="Normal" ItemsSource="{Binding Model.FooModels}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type models:FooModel}">
<DockPanel HorizontalAlignment="Left">
<CheckBox
HorizontalAlignment="Right"
Content="{Binding Name}"
DockPanel.Dock="Left"
IsChecked="{Binding IsFoo}" />
<TextBox
DockPanel.Dock="Right"
Style="{StaticResource SimpleTextBox}"
Text="{Binding FooStr}"
Visibility="{Binding IsFoo, Converter={StaticResource BooleanToVisibilityHideConverter}}" />
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
答案 0 :(得分:3)
只要数据绑定的源属性设置为新值,就应该实现INotifyPropertyChanged并向UI发出更改通知:
public class FooModel : Model, INotifyPropertyChanged
{
public int Id { get; set; }
public string Name { get; set; }
private bool _isFoo;
public bool IsFoo
{
get { return _isFoo; }
set { _isFoo = value; OnPropertyChanged(); }
}
public string FooStr { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}