How to Perform Label Visibility Binding according to the size of a List

Asked

Viewed 73 times

1

I have a list of objects of type "Basicvariable", a ObservableCollection of them, and I need the Graphical Interface to display a Label when the number of items in this Collection exceeds 1000, I think Binding is correct but I couldn’t find a way to update the counter and UI to each addition/removal of an object in this class...

Here’s the label I need to update:

<Label x:Name="configFilePath_Copy" Content="BasicVariable can't exceed 1000" HorizontalAlignment="Right" Margin="0,0,11,62" VerticalAlignment="Bottom" RenderTransformOrigin="0.5,0.5" Width="154" Foreground="Red">
  <Label.Style>
    <Style TargetType="{x:Type Label}">
      <Setter Property="Visibility" Value="Collapsed"/>
      <Style.Triggers>
        <DataTrigger Binding="{Binding BVExceed}" Value="True">
          <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Label.Style>
</Label>

Here I have placed a boolean property that returns whether or not it has more than 1000 objects

public bool BVExceed
{
  get { return NumberOfBasicVariablesExceeded(); }
  set
  {
    if(value != NumberOfBasicVariablesExceeded())
    {
      NumberOfBasicVariablesExceeded();
      RaisePropertyChanged("BVExceeded");
    }
  }
}

Here the method that interacts with the other viewmodel that possesses the observablecollection

public bool NumberOfBasicVariablesExceeded()
{
  if (BasicVariableViewModel != null)
  { return (BasicVariableViewModel.BasicVariables.Count >= 1000); }
  else
    return false; 
}

As I am new to the MVVM standard and WPF, I know my implementation is wrong in both the code and XAML, I would like to understand exactly what is the standard of doing this data triggers Binding to be able to implement correctly

1 answer

1


Dude, the question I see is that your variable BVExceed is not changed. Tries to put the method NumberOfBasicVariablesExceeded() together with the add or remove from your Collection and its return you assign in the variable BVExceed.

in case your Bvexceed would look more or less like this:

private bool _bvExceed;
public bool BVExceed
{
  get { return _bvExceed; }
  set
  {
      _bvExceed = value;
      RaisePropertyChanged("BVExceed");
  }
}

and within your add/remove method you call the NumberOfBasicVariablesExceeded()

BVExceed = NumberOfBasicVariablesExceeded();
  • I understood, but now I saw that there is a method that can use Oncollectionchanged, using a Notifycollectionchangedeventargs... Wouldn’t it be more correct to use it? I’m feeling that what I’m doing on the show is actually kind of funny

Browser other questions tagged

You are not signed in. Login or sign up in order to post.