1

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:

  1. WPF Datagrid Get Selected Cell Value
  2. Dynamically Changing Combobox to TextBox in datagrid inside Cell Editing Template in silverlight 4
  3. 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...

Kevin Avignon
  • 2,853
  • 3
  • 19
  • 40
  • Maybe this is what you are looking for: https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-find-datatemplate-generated-elements. No idea if in Silverlight works... – Marco Mar 01 '18 at 15:58
  • Sorry but it sounds like you're going about it all wrong.... your button (and stack panel) already have their `Visibility` property bound to some data. So, you don't really need a `DisableWriteableRights` method for that. All you really need is just to make sure your data correctly reflects who has and who doesn't have proper rights, and the buttons will collapse (or not) _"automagically"_. – jsanalytics Mar 01 '18 at 21:51
  • @jsanalytics Instead, I created my own converter for admin rights and got around my issue. If you have a better approach, could you post it because I'm not really sure what you meant in your comment ! – Kevin Avignon Mar 01 '18 at 22:16
  • Well... if you solved your problem then it's all good. Read about MVVM and my comments will become clear...:) – jsanalytics Mar 01 '18 at 22:21
  • Oh I read way too fast wha you wrote.. That's on me haha. Yeah, your comment does make sense. Some part of the codebase I'm maintain uses MVVM properly and some doesn't... I think a refactoring is in order and that'll remove the need for my converter – Kevin Avignon Mar 01 '18 at 22:24
  • BTW, if you really solved your problem, then you should post your answer, so others can benefit from it. – jsanalytics Mar 01 '18 at 23:02

0 Answers0