Tea With Cookies
Tea With Cookies

Reputation: 1020

Changing button content according to DataGrid SelectedItem property

I have a WPF Window and a Viewmodel and I need to change the content of a button to fit the property of the SelectedItem in the Datagrid.

For example:
The DataGrid is bound to a list of tasks with two properties: Name and EndDate. If EndDate is null I want the button to display 'End Task' and if it is not null to display 'Resume Task'
I managed to hide the button if SelectedItem is null, but I can't figure out a way to do this.

Here is the XAML code that I have for the button:

<DataGrid AutoGenerateColumns="False" 
          ItemsSource="{Binding Tasks}"  
          x:Name="dg" 
          SelectedItem="{Binding SelectedTask}">
          <DataGrid.Columns>
              <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
              <DataGridTextColumn Header="End Date" Binding="{Binding EndDate}"/>
          </DataGrid.Columns>
</DataGrid>

<Button Content="{Binding ButtonText}"
        Name="btn_close_resume"
        Command="{Binding CloseResumeCommand}"
        CommandParameter="{Binding ElementName=dg, Path=SelectedItem}">
        <Button.Style>
            <Style TargetType="Button">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SelectedTask}" Value="{x:Null}">
                        <Setter Property="IsEnabled" Value="False"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
</Button>

On the ViewModel I have the ButtonText property like this:

public string ButtonText
{
      get { return this.SelectedTask.EndDate == null ? "Close Task" : "Resume Task"; }
}

But the Button doesn't display any text.
How can I do this?

Upvotes: 1

Views: 971

Answers (1)

The One
The One

Reputation: 4784

You can use another DataTrigger to set the content on the button based on the SelectedTask EndDate value

<Button
    Name="btn_close_resume"
    Command="{Binding CloseResumeCommand}"
    CommandParameter="{Binding ElementName=dg, Path=SelectedItem}">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Content" Value="Resume Task"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SelectedTask}" Value="{x:Null}">
                        <Setter Property="IsEnabled" Value="False"></Setter>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding SelectedTask.EndDate}" Value="{x:Null}">
                        <Setter Property="Content" Value="Resume Task"/>
                    </DataTrigger>

                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

How ever as you didn't say what's the default value of the button's content when there's nothing selected on the datagridview, the button will say "Resume Task" until something gets selected. If you need to change this, you will need to use a converter.

If you really want to do it through the view model, you have to notify the changes in the SelectedTask setter:

 public Task SelectedTask
    {
        get { return selectedTask; }
        set
        {
            selectedTask = value;
            OnPropertyChanged("SelectedTask");
            OnPropertyChanged("ButtonText");
        }
    }


public string ButtonText
{
  get { return this.SelectedTask.EndDate == null ? "Close Task" : "Resume Task"; }
}

Upvotes: 2

Related Questions