How to set cell template control visibility from c# code.

Mar 23, 2010 at 11:54 AM

Hi,

I am using Datagrid view and used the DataGridTemplatecolum with datatemplate containg one image, text and two hyperlinks. I want to disable and enable the visibility of hyperlinks based on the some other data for that row. I am trying to do this in LoadingRow event but seems missing something. The XAML code is given below. Please help me on this.

 

<WpfToolkit:DataGridTemplateColumn Header="TestText" x:Name="citeGridTestTextCol" Width="300">

                <WpfToolkit:DataGridTemplateColumn.CellTemplate>

                    <DataTemplate x:Name="TestTemplate">

                        <Grid Width="285">

                            <Grid.ColumnDefinitions>

                                <ColumnDefinition Width="Auto"/>

                                <ColumnDefinition  />

                                <ColumnDefinition />

                            </Grid.ColumnDefinitions>

                            <Grid.RowDefinitions>

                                <RowDefinition />

                                <RowDefinition />

                            </Grid.RowDefinitions>

                            <Grid Grid.Column="0" Grid.Row="0">

                                <Grid.ColumnDefinitions>

                                    <ColumnDefinition/>

                                </Grid.ColumnDefinitions>

                                <Grid.RowDefinitions>

                                    <RowDefinition/>

                                </Grid.RowDefinitions>

                                <TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Top" Margin="2,0,0,0">

                                    <Image Source="green.gif" Stretch="None"></Image>

                                </TextBlock>

                            </Grid>

                            <TextBlock Text="Hello this is just test description" FontSize="12" Margin="5,0,0,0"

                                       TextWrapping="Wrap" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2" Loaded="TextBlock_Loaded">                               

                            </TextBlock>

                            <Label  FontSize="10" HorizontalAlignment="Right"

                                     Grid.Column="1" Grid.Row="1" VerticalAlignment="Bottom">

                                <Hyperlink NavigateUri="https://mvp.support.microsoft.com/profile/Alessandro.Del%20Sole"

                                     >

                                    <TextBlock Text="Accept match" />

                            </Hyperlink>

                            </Label>

                            <Label  FontSize="10" HorizontalAlignment="Left" VerticalAlignment="Bottom"

                                   Grid.Column="2" Grid.Row="1">

                                <Hyperlink NavigateUri="https://mvp.support.microsoft.com/profile/Alessandro.Del%20Sole"

                                 >

                                    <TextBlock Text="More matches" />

                             </Hyperlink>

                            </Label>

                        </Grid>

                    </DataTemplate>

                </WpfToolkit:DataGridTemplateColumn.CellTemplate>

 

Mar 29, 2010 at 6:13 AM
Edited Mar 29, 2010 at 6:14 AM

Hi,

 

I have got the answer. We can bind the control property with the help of converter. In the converter we can pass the full object or single property and based on the logic we can return the Visibility.Hidden or Visible enum to the control. To implement this steps are given below.

1. Create one class in the code behind and implement the Iconverter interface.

 [ValueConversion(typeof(<TypeOFObjet or String or any other type whatever you are binding with the control in XAML>),typeof(Enum))]
    public class GridVisibility: IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
           if (<Condition>)
            {
                return Visibility.Hidden;
            }
            else
                return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

 

2. Specify the resource in UserControl or window xaml.

 

<UserControl.Resources>

<local:GridVisibilityx:Key="GridVisibility">

</local:AcceptAndMoreMatchGridVisibility>

</UserControl.Resources>

 3. In the XAML specify the converter binding.

Visibility

= "{Binding <object or property of the data class.>, Converter={StaticResource <Name of the resource mentioned in the step 2.>}}

Thanks!!!!!!!