Home > Enterprise >  Bind to the Visibility Property of a Label to a Custom Property
Bind to the Visibility Property of a Label to a Custom Property

Time:12-03

I have a function in my UI where I want to be able to collapse/make visible a text message depending on the value of a custom property in my window object.

Using online references, I have come up with this code-behind to register the property:

public bool ValidInterval
{
   get { return pValidInterval; }
}
private bool pValidInterval = true;
public static readonly DependencyProperty ValidIntervalProperty = DependencyProperty.Register("ValidInterval", typeof(bool), typeof(Settings), new UIPropertyMetadata(true));

And this corresponding XAML for the label:

<Label Name="DynamicWarning" Content="Time interval must be a valid positive integer.">
   <Label.Style>
      <Style TargetType="Label">
         <Setter Property="Visibility" Value="Collapsed" />
         <Style.Triggers>
            <DataTrigger Binding="{Binding ValidInterval}" Value="true">
               <Setter Property="Visibility" Value="Collapsed" />
            </DataTrigger>
            <DataTrigger Binding="{Binding ValidInterval}" Value="false">
               <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Label.Style>
</Label>

Unfortunately, this does not work. I can get it to set the parameter depending on the initial value of the property, but doesn't update the visibility dynamically like I want. I have been looking at this for an hour and what I have seems consistent with examples I am finding online for similar operations. Am I doing something wrong, or can you not update the visibility on the fly? If the latter, how do I achieve an equivalent effect?

CodePudding user response:

ΩmegaMan's answer is correct. But I wanted to clarify, you don't need a backing field with a dependency property, the static dependency property IS the backing field.

public bool ValidInterval
{
   get { return (bool)GetValue(ValidIntervalProperty); }
   set { SetValue(ValidIntervalProperty, value); }
}

However, if you aren't in need of a dependency object specifically, then you may just want to use INotifyPropertyChanged as ΩmegaMan has it right. Dependency properties are typically used when you need to bind another property to them, such as when making your own custom control. For example Visibility itself is the dependency property in your example and ValidInterval just needs to be a normal property that invokes the NotifyPropertyChanged event.

CodePudding user response:

You need for the holder class of the properties which are bound to the page, to adhere to INotifyPropertyChanged Interface and implement it.

That process informs the bound controls on the page that something has changed, and when it has changed, then the control is "notified" of the change; then it reads afresh the property it is bound to.


For WPF/Xaml they specify the seperation of data concerns for the views to business logic, is done by implementing the Model-View-ViewModel or MVVM pattern.

The link provided is dry, and there are other resources which can describe on the net, but it simply says put all your business logic that is bound from the View to a separate View Model Class; which is instantiated on your View.

I provide a basic example, any version of .Net can be used, on my blog:

MVVM Example for Easier Binding

  • Related