-1

Problem I encountering is that my TextBox doesn't hide nor becoming visible when I'm changing ObservableCollection bool value IsFoo. I can't figure out why is this happening. Am I missing something?

Model using for 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; }
    }

Model using between ViewModel and 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);
      } 
    }

View

<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>

Daniel Žeimo
  • 173
  • 4
  • 14
  • `FooModel.IsFoo` doesn't fire the `PropertyChanged` event, according to your code. By the way, the `Model` class is wrong: it cannot derive from itself. – dymanoid Sep 26 '18 at 13:22
  • @dymanoid In case of Model posting code here just named wrong, already corrected. If you mean that i need `UpdateSourceTrigger=PropertyChanged` first attempt was with property changed but still no effect – Daniel Žeimo Sep 26 '18 at 13:27
  • What dymanoid means is that your properties don't **fire** the changed event. "UpdateSourceTrigger" has nothing to do with that – Sentry Sep 26 '18 at 13:31
  • @Sentry I see, but why properties don't fire? I wrote this code from my other example which works perfectly difference only that example not using `ObservableCollection` – Daniel Žeimo Sep 26 '18 at 13:33
  • The properties don't fire the `PropertyChanged` event because you didn't implement that in your `FooModel` class. – dymanoid Sep 26 '18 at 13:36
  • @dymanoid Am I understood it right and this must look something like [here](https://stackoverflow.com/questions/14869928/adding-inotifypropertychanged-to-model)? – Daniel Žeimo Sep 26 '18 at 13:42
  • 1
    A good overview of various techniques can be found [here](https://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist), I prefer [this one](https://stackoverflow.com/a/18229778/391338) that sets the value and fires the event in one method call – Sentry Sep 26 '18 at 14:00

1 Answers1

3

You should implement INotifyPropertyChanged and raise change notifications to the UI whenever the data-bound source property is set to a new value:

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));
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88