How to add binding to VisibilityProperty of DataGridRow?

Nov 26, 2008 at 2:31 AM
Can I bind the VisibilityProperty of DataGridRow to some property of source data class in XAML?
Now I add the binding in the event handler of LoadingRow.
I don't know whether there are better way.
Following is my current code.



        private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            //Get DataGridRow object
            DataGridRow currentDataGridRow = (DataGridRow)e.Row;

            //Get DataGrid object
            DataGrid parentDataGrid = (DataGrid)sender;

            //Get the DataGridRow's data item object
            DataItemType currentItem = (DataItemType)currentDataGridRow.Item;

            //Bind the Visibility of Data Item to its DataGridRow
            Binding myBinding = new Binding("IsVisible");
            myBinding.Source = currentItem;
            currentDataGridRow.SetBinding(VisibilityProperty, myBinding);
Coordinator
Nov 26, 2008 at 3:03 AM
Something like the following should work for you....

        <Style x:Key="DataGridRowStyle" TargetType="{x:Type dg:DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsVisible}" Value="False">
                    <Setter Property="Visibility" Value="Collapsed"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <dg:DataGrid RowStyle={StaticResource DataGridRowStyle} ... />
Nov 26, 2008 at 7:26 AM
Thanks a lot.
This is a better way, and it works.