In the Silverlight application I'm maintaining, I'm having a small roadblock. In my XAML UserControl, I have a standard Grid that holds a DataGrid. I'm trying to access the children of my Datagrid. Maybe the formulaion isn't the most precise in my given context, I'm still learning about WPF so you'll have to excuse me.
Here's the context. Inside my Datagrid, we've defined the template of how the cells are going to look like in the DataGridTemplateColumn. Such as
<DataGrid>
<DataGrid.Columns>
<DatagridTemplateColumn BindingHelper = "Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation = "Horizontal" Visibility="{Binding IsApproved, Converter={StaticResource BoolToVisibilityConverter}>
<-- I want to access this button for instance -->
<Button Command="DoingStuffCommand" Name="DoingStuffA" Visibility="{Binding DoingStuffCommandVisible, Converter={StaticResource BoolToVisibilityConverter}}" />
<-- There are more than one buttons in this stackpanel -->
.....
</DataGridTemplateColumn/>
Inside my StackPanel, I have Button instances I'd like to render "Collapsed" whenever a certain condition is met, such as you don't have the admin rights to be able to modify the page so you only have read-only rights.
I've designed a method for this problem that I call DisableWriteableRights() where I first get my grid and look for my datagrid which is one of my children. While the page is being constructed, the method would take a look at your profile's rights and determine whether or not you have admin access or not.
There aren't any cells being selected.
I took a quick peek here:
- WPF Datagrid Get Selected Cell Value
- Dynamically Changing Combobox to TextBox in datagrid inside Cell Editing Template in silverlight 4
- Programmatically set content in Silverlight DataGrid details
But this doesn't give me really what I'd like. I'm not selecting a cell, I'm creating my view and while creating it, I'm deciding whether or not the buttons should be collapsed or not. Even if collapsed, the columns would still remain in the view, but they'll be empty.
Now, taking a look at the given properties of a Datagrid, I do not know what to do in order to get the children of the StackPanel that are inside my DataGrid. I could say that part of my issue is that I'm not starting from a DataGridRow. From there, I could provide that reference to GetCellContent to a DataGridColumn, but this isn't my context. Instead of moving up in my hierarchy, I have to move down, and I don't see any way, right now, to do so. The only thing that made sense for me was to take a look at the Columns property and try to find the column that interested me. My problem is to how to move from there to the cells that hold a Button reference and how to effectively "collapsed" them.
Here's a snippet to my method :
private void DisableWriteableRights()
{
var grid = Content as Grid;
if(grid == null)
return;
var dataGrid = grid.Children.Cast<UIElement>().FirstOrDefault(x => x is DataGrid) as DataGrid;
if(dataGrid == null)
return;
var actionColumn = dataGrid.Columns.FirstOrDefault(c => ((string)c.Header).Equals("Action", StringComparison.InvariantCultureIgnoreCase));
//Looking for how to access the buttons in my datagrid...
//buttons.ForEach(button => button.Visibility = Visibility.Collapsed);
}
P.S Something I'd also like to from a column, is there any way to get buttons instances instead of using something like FindName because I find it'll get tedious and if names changes over time, you'll have breaking changes as well...