Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
40
Xamgrid DataCell Dynamic Visibilty
posted

Hi Team,

I have an silverlight application in which i am using a xamgrid. 

In the xam grid i am using two columns with the edit and delete buttons which are user specific. I am trying to enable them from code behind with the help of visibility converter but no luck,

<ig:XamGrid x:Name="Details"
                    Width="auto"
                    Margin="0.3"
                    Background="Gainsboro"
                    BorderBrush="LightSlateGray"
                    AutoGenerateColumns="False"
                    ColumnWidth="Auto"
                    BorderThickness="1"
                    ScrollViewer.VerticalScrollBarVisibility="Auto" 
                    ScrollViewer.HorizontalScrollBarVisibility="Auto"
                    Grid.Row="1">
                      
                        <ig:XamGrid.Columns>
                            <ig:TextColumn HeaderText="User" MinimumWidth="200"
                               Key="UserName" IsReadOnly="True">
                                <ig:TextColumn.FilterColumnSettings>
                                    <ig:FilterColumnSettings>
                                        <ig:FilterColumnSettings.FilteringOperand>
                                            <ig:ContainsOperand />
                                        </ig:FilterColumnSettings.FilteringOperand>
                                    </ig:FilterColumnSettings>
                                </ig:TextColumn.FilterColumnSettings>
                            </ig:TextColumn>
                           
                            <ig:TextColumn HeaderText="By" MinimumWidth="200"
                               Key="By" IsReadOnly="True">
                                <ig:TextColumn.FilterColumnSettings>
                                    <ig:FilterColumnSettings>
                                        <ig:FilterColumnSettings.FilteringOperand>
                                            <ig:ContainsOperand />
                                        </ig:FilterColumnSettings.FilteringOperand>
                                    </ig:FilterColumnSettings>
                                </ig:TextColumn.FilterColumnSettings>
                            </ig:TextColumn>
                           
                            <ig:UnboundColumn Key="Edit" >
                                <ig:UnboundColumn.ItemTemplate>
                                    <DataTemplate >
                                        <Button Content="Edit" Visibility="{Binding Path=IsEditable,Converter={StaticResource BoolToVisibilityConverter},Source={Binding}}" CommandParameter="{Binding}" Click="Button_Click"/>
                                    </DataTemplate>
                                </ig:UnboundColumn.ItemTemplate>
                            </ig:UnboundColumn>
 
                            <ig:UnboundColumn Key="Delete" >
                                <ig:UnboundColumn.ItemTemplate>
                                    <DataTemplate>
                                        <Button Content="Delete" Visibility="{Binding IsAdmin,Converter={StaticResource BoolToVisibilityConverter}}" CommandParameter="{Binding}" Click="Button_Click_1"/>
                                    </DataTemplate>
                                </ig:UnboundColumn.ItemTemplate>
                            </ig:UnboundColumn>
                        </ig:XamGrid.Columns>
                        <ig:XamGrid.EditingSettings>
                            <ig:EditingSettings AllowEditing="Cell"
                                    IsEnterKeyEditingEnabled="True"
                                    IsF2EditingEnabled="True"
                                    IsMouseActionEditingEnabled="SingleClick"
                                    IsOnCellActiveEditingEnabled="True" />
                        </ig:XamGrid.EditingSettings>
                    </ig:XamGrid>

BoolToViisibilty code:
public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((bool)value)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Can you debug and attach an working example please.

Parents
No Data
Reply
  • 34430
    Offline posted

    Hello Ajay,

    Thank you for your post.

    I have been investigating the code that you have provided, and I am assuming that the IsEditable and IsAdmin properties that you are trying to bind your button's visibility properties to are properties on your underlying data item. Please let me know if this is not the case, as the following goes off of that impression.

    When placing an element in an ItemTemplate of an UnboundColumn, the data context of those elements behaves a little bit differently. Since the column is "unbound," this can be translated as not having its data context bound directly to the underlying data item represented by the row in the XamGrid. Instead, it is assigned an UnboundColumnDataContext object as its data context. This UnboundColumnDataContext object has a property named RowData that will allow you to obtain the underlying data item for the XamGrid Row.

    If you take a look at the output window in Visual Studio when you run your application, I imagine you would likely see something along the lines of "BindingExpression error: the IsAdmin/IsEditable property does not exist on the item UnboundColumnDataContext." This is the reason you are seeing the behavior you are seeing, and it is happening because you are trying to bind to the IsAdmin and IsEditable property of the UnboundColumnDataContext object, which doesn't exist. I would recommend that you refactor your binding paths to be RowData.IsAdmin and RowData.IsEditable. This should resolve the issue that you are seeing.

    I have attached a sample project to demonstrate the above. I hope this helps you.

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer
    Infragistics Inc.
    www.infragistics.com/support

    XamGridButtonVisibilityCase.zip
Children
No Data