I have a RadGridView with a custom row style targeting GridViewRow (set using RowStyle property and defined in resource dictionary).
In that style I have a control template targeting GridViewRow.
In that template I have a couple of GridViewCells definitions.
Here is one of them, for example:
<telerik:GridViewCell x:Name="theCell" |
BorderThickness="0" |
Foreground="{StaticResource ColorMessageText}" |
Value="{Binding Owner}" |
Margin="0,0,0,2" |
Content="Owner" |
Background="{x:Null}" |
BorderBrush="{x:Null}" |
FontSize="11" |
HorizontalAlignment="Left" |
HorizontalContentAlignment="Left" |
Width="90" |
Visibility="{Binding Column.IsVisible, Converter={StaticResource VisibilityOfBool}, ElementName=theCell}"/> |
Now, what I am trying to accomplish is to bind the Visibility property to the column IsVisible property (using the required converter).
The problem is that both Column and DataColumn properties of the GridViewCell are unknown at run time (saw it using Snoop).
How do I make my GridViewCell hide when the matching column is hidden?
Thanks,
Gili
8 Answers, 1 is accepted
It is not recommended to define cells of the Grid in that way. Please, give us more details about your scenario so that we are able to provide you with a possible solution.
Furthermore, there are two predefined converters - BooleanToVisibilityConverter and InverterBooleanToVisibilityConverter that you can easily use instead of creating your own. What you need to do in order to use them is:
- Add:
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
2. Define them in the Resources:
<
telerik:BooleanToVisibilityConverter
x:Key
=
"BooleanToVisibilityConverter"
/>
<
telerik:InvertedBooleanToVisibilityConverter
x:Key
=
"InvertedBooleanToVisibilityConverter"
/>
3. Use them in the Binding definition of the element you want to apply the converter to.
Best wishes,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
I know about the built-in Telerik converters and I am using them. Thanks.
As for the project, it is very similar to what you have in your blog, which does use GridViewCell:
http://blogs.telerik.com/blogs/posts/09-02-03/how_to_use_custom_row_layouts_in_radgridview_for_wpf.aspx
Have you removed the recommendation on using GridViewCell since?
What is the recommended approach then to custom row styles and how can I locate the
column.IsVisible from within the row style template?
Thanks,
Gili
The blog post that you are using as a reference is out-of-date and now it is not a recommended approach.
However, what you can do in order to bind the visibility of your element you want to hide with the visibility of the column is:
<
TextBlock
Name
=
"myTextBlock"
Text
=
"test"
Visibility
=
"{Binding Columns[0].Visibility, ElementName=RadGridView1 , Converter={StaticResource VisibilityConverter}}"
/>
In this case the element is TextBlock, but you can rearrange it according to your scenario.
I hope that helps and if you have any more questions, please do not hesitate to get back to us.
All the best,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
One more question though, how do I access the specific column by name from the binding?
I believe the index access as you proposed may not work as the user can change the column order.
Visibility="{Binding Columns[?].Visibility, ElementName=RadGridView1 , Converter={StaticResource VisibilityConverter}}" |
Thanks,
Gili
What you can do in this case is to define the property of the column UniqueName. Thus you can use it instead of the index. For example:
<
telerik:GridViewColumn
Header
=
"Country"
UniqueName
=
"CountryName"
>
<
telerik:GridViewColumn.CellTemplate
>
<
DataTemplate
>
<
TextBlock
Name
=
"myTextBlock"
Text
=
"{Binding Country}"
Visibility
=
"{Binding Columns[CountryName].Visibility, ElementName=RadGridView1, Converter={StaticResource VisibilityConverter}}"
/>
</
DataTemplate
>
</
telerik:GridViewColumn.CellTemplate
>
</
telerik:GridViewColumn
>
Best wishes,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
I had to perform some modifications on the sample code you posted to make it work, but it does work as follows:
Visibility="{Binding Columns[Message].IsVisible, RelativeSource={RelativeSource AncestorType=telerik:RadGridView, Mode=FindAncestor}, Converter={StaticResource VisibilityOfBool}}"> |
Later I tried the same code when the columns are defined in code (in the user control's constructor) and it didn't work.
I wonder if there is a precedence issue here - what gets executed first? the code in the user control's constructor creating the columns, or the XAML template binding the visibility as above?
Any idea you may have can be useful.
Thanks,
Gili
Basically, the first to be executed is the xaml file, but everything depends on what you have written in the code-behind. Thus there is no straight forward way to say which of the two is the first one.
There are a couple of ways of sequences to create your columns and to bind an item to their visibility. In you case you can define all you need in xaml or in the code-behind. Another approach is to define the columns in xaml, give them a specifier by means of UniqueName or x:Name and use it in the code behind.
So, please give us more details about the way you want to define your columns and to bind an element to their visibility so that we can be more helpful.
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Once we bound visibility to the column itself with an appropriate converter it worked.
Gili