Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

DataGridViewCell binding #116

Open
simpledevelop opened this issue Apr 16, 2019 · 1 comment
Open

DataGridViewCell binding #116

simpledevelop opened this issue Apr 16, 2019 · 1 comment

Comments

@simpledevelop
Copy link

How can I add a cell with two labels, one visible if the row is selected and one not visible when the row is not selected?
Thanks!

@fedemkr
Copy link

fedemkr commented Jun 6, 2019

I'd create a DataGridColumn like this:

<dg:DataGridColumn PropertyName=".">
    <dg:DataGridColumn.CellTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding MyItem.Description}" />
                    <Label IsVisible="{Binding IsSelected}" Text="{Binding MyItem.HiddenDescriptionUntilSelected}" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>

Bind the SelectedItem to the ViewModel and in your ViewModel:

public class MyViewModel : BaseViewModel
{
    public ObservableCollection<MyItemViewModel> MyItems = new ObservableCollection<MyItemViewModel>();

    private MyItemViewModel _selectedItem;
    public MyItemViewModel SelectedItem
    {
        get => this._selectedItem;
        set
        {
            this._selectedItem = value;
            foreach (var i in this.MyItems)
                i.IsSelected = false;

            this._selectedItem.IsSelected = true;
        }
    }
}

public class MyItemViewModel : BaseItemViewModel
{
    public MyItem MyItem { get; set; }

    public bool IsSelected { get; set; }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants