> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/gum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/gum/code/binding-viewmodels/view-model-property-dependency.md).

# View Model Property Dependency

## Introduction

Gum's `ViewModel` supports using the `DependsOn` attribute to define dependencies between properties. By using this attribute, changes in one property can result in changes to other properties. The same property can be used as a dependency for multiple properties, allowing changes to one property resulting in many pieces of UI being updated.

## Code Example: Using DependsOn

The following code shows how a single variable can be used to update multiple UI properties. First we can declare a ViewModel using `DependsOn` to make `IsBrokeTextVisible` and `MoneyDisplay` depend on `Money`.

```csharp
class PlayerViewModel : ViewModel
{
    public int Money
    {
        get => Get<int>();
        set => Set(value);
    }

    [DependsOn(nameof(Money))]
    public bool IsBrokeTextVisible => Money <= 0;

    [DependsOn(nameof(Money))]
    public string MoneyDisplay => $"${Money:N0}";
}
```

We can use the `PlayerViewModel` to update the UI in response to `Money` changing. The following block of code shows how to do so in a code-only project.

```csharp
// Initialize
var viewModel = new PlayerViewModel();

var mainPanel = new StackPanel();
mainPanel.AddToRoot();
mainPanel.BindingContext = viewModel;

var addMoneyButton = new Button();
mainPanel.AddChild(addMoneyButton);
addMoneyButton.Text = "+";
addMoneyButton.Click += (_, _) =>
    viewModel.Money += 100;

var subtractMoneyButton = new Button();
mainPanel.AddChild(subtractMoneyButton);
subtractMoneyButton.Text = "-";
subtractMoneyButton.Click += (_, _) =>
    viewModel.Money -= 100;

var moneyLabel = new Label();
mainPanel.AddChild(moneyLabel);
moneyLabel.SetBinding(
    nameof(moneyLabel.Text),
    nameof(viewModel.MoneyDisplay));

var isBrokeLabel = new Label();
mainPanel.AddChild(isBrokeLabel);
isBrokeLabel.Text = "No more money!";
isBrokeLabel.SetBinding(
    nameof(isBrokeLabel.IsVisible),
    nameof(viewModel.IsBrokeTextVisible));
```

[Try on XnaFiddle.NET](https://xnafiddle.net/#code=H4sIAAAAAAAACp1WUW_aMBB-z6_wUB8SlUZUeyul0gAVIY2uWimbNE2VSQ5q4djIdsJYxX-f7QSThDDW5SXm7vPdfXcfB6kkbIkmnPERTmCUJl0vtSZ9DO-5SOSRIRxwpgSnDZ4hLHBK1YzIFFMZzj6WIZMsq0T_RgQshM66N05IJLjkCxV-Zzi8N64NF6sz7nAk8PqVRLocb53OKYlQRLGU6JHiLYgZgc2Ex0DRDXJn781D-inghCnTAdhaY-4yzxIU6t2hEahbDbnzg65zydz1BMrPME2hcO08-_oxhDWwWH5hPtNV8oVvwwfBz3LaOecUjWVf8BVM4ZfpGplTMHEtHN32UKf7nohSiWKasB0SudYNMNEuWhdv1nbz0Nm1ut6u1igz-WvdHvMuOrPv6RAyEsEEM7wEgZau0xaTJk8gjN8cn8e2V86210JBoEhoU_kBenOxUA8x2DQn9NUrkUFXN2nCUwmuQUiJFLr7bq8FVxApiBHPQAgSA8o4idGYEUUwJb_BD2qTtfWGJYBJ1EZV-c5ASMKZVrGerruaYYEyp6m8-JrS_Do-wYQ9YubwTwpHK2soa8qhwk9xPOVfOVfN7j5hsZ6z-Rpq2eiYrp5aXhzHdu79VCnOiuT5h5OJB6-Exn71ZglbdYRGtzpu67J1EjLQc1-hyx7yX9roJdAqcUhb5772MBe9Bl53OjUiMp0rgSP1n2warpe_y8dex-uq9XfcO8ldNZFLjOsznjt52PMZSodLZZwzhnozFTLxKxUV26OENFSDdhOoVn2xUYK6ukm-wt7LoHythCyb3RgeuOYmICf4oXUCfYZzBTuWxTY5R_14QVcaMMcSyouk-kNwajU9r2OswDfbcEoSQMvi0LynCrQDHejb7M3uMxUMBd78Q_7KVtZqByz8Aadc6D8Bgi0o34DoH37_DjXb-PVKrfGozp33B8hOO_SFCAAA)

If this code were in a Screen defined in Gum with code generation, the code might look like the following block:

```csharp
void CustomInitialize()
{
    var viewModel = new PlayerViewModel();
    // Can be added to the entire screen:
    this.BindingContext = viewModel;
    
    AddMoneyButton.Click += (_, _) =>
        viewModel.Money += 100;
        
    SubtractMoneyButton.Click += (_, _) =>
        viewModel.Money -= 100;
        
    MoneyLabel.SetBinding(
        nameof(MoneyLabel.Text),
        nameof(viewModel.MoneyDisplay));
        
    IsBrokeLabel.SetBinding(
        nameof(IsBrokeLabel.IsVisible),
        nameof(viewModel.IsBrokeTextVisible));
}
```

<figure><img src="/files/pQZRkrNJDPUjOLbQ5n0v" alt=""><figcaption><p>Two buttons updating money</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flatredball.com/gum/code/binding-viewmodels/view-model-property-dependency.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
