This is a migrated thread and some comments may be shown as answers.

How to best access the rows selected state from a CellTemplate?

1 Answer 130 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 26 Sep 2012, 06:49 PM
I'm using RadGridView with a GridViewDataColumn defined with custom CellTemplate and CellEditTemplate defined. What I would like to do now is affect something defined in my DataTemplate for the CellTemplate based on whether it's row is selected or not.

More specifically in my example I have a DataTemplate that contains a TextBlock with TextTrimming and TextWrapping set up to show the text as a single line with character ellipsis when there is overflow. What I would like to do is display the text with TextWrapping turned on and without TextTrimming which I could do various ways if I can access the selection state of the parent row for that cell.

<telerik:GridViewDataColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Note}"
               TextWrapping="NoWrap"
               TextTrimming="CharacterEllipsis" />
    </DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>

What's the best way for me to accomplish this with the RadGridView control? Is there an inherited attached property I can use or will a relative binding work. I would prefer doing this through a defined style with triggers on my TextBlock if possible to avoid writing value converters.

1 Answer, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 26 Sep 2012, 07:27 PM
I believe I've figured it out on my own however I would be itnerested in any feedback I could get on whether this is the best way to achieve what I'm trying to do.

<telerik:GridViewDataColumn.CellTemplate>
  <DataTemplate>
      <TextBlock>
          <TextBlock.Style>
              <Style TargetType="TextBlock">
                  <Setter Property="Text"
                       Value="{Binding Note}" />
                  <Setter Property="TextWrapping"
                       Value="NoWrap" />
                  <Setter Property="TextTrimming"
                       Value="CharacterEllipsis" />
                   
                  <Style.Triggers>
                      <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=telerik:GridViewRow}}"
                              Value="True">
                          <Setter Property="Text"
                               Value="{Binding Note}" />
                          <Setter Property="TextWrapping"
                               Value="Wrap" />
                          <Setter Property="TextTrimming"
                               Value="None" />
                      </DataTrigger>
                  </Style.Triggers>
              </Style>
          </TextBlock.Style>
      </TextBlock>
  </DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
Tags
GridView
Asked by
Jeff
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Share this question
or