Cristian
Cristian

Reputation: 285

DataGrid row datatrigger/template

I have the following DataGrid:

<DataGrid ItemsSource="{Binding Coll.View}" SelectedItem="{Binding SelectedTransaction}"
          ScrollViewer.CanContentScroll="True" 
          ScrollViewer.VerticalScrollBarVisibility="Visible"
          AutoGenerateColumns="False">

    <DataGrid.Columns>
        <DataGridTextColumn Header="Date" Binding="{Binding Date}" Width="100" />
        <DataGridTextColumn Header="Category" Binding="{Binding Path=SelectedCategory.CategoryName}" Width="100" />
        <DataGridTextColumn Header="Payee" Binding="{Binding Path=SelectedPayee.PayeeName}" Width="100" />
        <DataGridTextColumn Header="Withdrawal"  Width="100" />
        <DataGridTextColumn Header="Deposit"  Width="100" />
        <DataGridTextColumn Header="Total" Binding="{Binding AccountBalance}" Width="100" />
    </DataGrid.Columns>

Transactions is an ObservableCollection of TransactionViewModel items.

The items have this property which is an enum that can be either : Deposit, Withdrawal.

private TransactionTypes tType;
public TransactionTypes TransactionType
{
    get { return tType; }
    set
    {
        tType = value;
        OnPropertyChanged("TransactionType");
    }
}

and this one :

public double? TransactionAmmount
{
    get { return ammount; }
    set
    {
        ammount = value;
        OnPropertyChanged("TransactionAmmount");
    }
}

Is it possible to bind the TransactionAmmount based on the value of TransactionType? If TransactionType is Deposit then bind the TransactionAmmount to the Deposit datagrid column.

If TransactionType is Withdrawal then bind the the TransactionAmmount to the Withdrawal column.

Upvotes: 0

Views: 196

Answers (1)

Philip Stuyck
Philip Stuyck

Reputation: 7467

You have to use the ElementStyle and EdittingElementStyle to achieve what you want in combination with triggers :

<DataGridCheckBoxColumn Header="OK" Binding="{Binding Goedgekeurd,UpdateSourceTrigger=PropertyChanged}"
                                    ElementStyle="{StaticResource GoedkeuringCheckBoxStyle}"
                                    EditingElementStyle="{StaticResource GoedkeuringCheckBoxStyle}"/>

The styles are defined upfront in the resources :

<Style x:Key="GoedkeuringCheckBoxStyle" TargetType="CheckBox">
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsWachtOpGoedkeuringKlant}" Value="False">
                <Setter Property="IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

The examples here are taken from my code I did not change them to fit your question exactly but I hope you can follow what is happening and adjust it to your needs. The point is that the binding is always on row level.

But if you use MVVM then you can do the logic in the viewmodel and not in the view. That could make things more simple.

Upvotes: 1

Related Questions