1
votes

I am following the example posted by Telerik on how to show/hide columns in their RadGridView control, as shown here:

  <StackPanel x:Name="CustomizeGrid" Background="Transparent" Orientation="Horizontal">
    <ListBox ItemsSource="{Binding Columns, ElementName=WorklistGridView}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Header}" IsChecked="{Binding IsVisible, Mode=TwoWay}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <telerik:RadGridView x:Name="WorklistGridView" AutoGenerateColumns="False" RowIndicatorVisibility="Collapsed" IsReadOnly="True" SelectionMode="Multiple" 
                     CanUserSelect="False" IsSynchronizedWithCurrentItem="False" ItemsSource="{Binding Mode=OneWay}" IsFilteringAllowed="True">
        <telerik:RadGridView.Columns>
            <telerik:GridViewSelectColumn x:Name="Select" IsResizable="False" />
            <telerik:GridViewDataColumn Header="Status" DataMemberBinding="{Binding OrderStatusDescription}"/>
            <telerik:GridViewDataColumn Header="Patient Name" DataMemberBinding="{Binding PatientName}"/>

But the example is not compiling correctly. The problem is here: CheckBox `Content="{Binding Header}" The main error listed is: Value does not fall within the expected range.

I'm not sure why this is happening. I'll try to post the rest of the error below. Does anyone else have this working, or have any ideas what's up?

System.InvalidOperationException

An unhandled exception was encountered while trying to render the current silverlight project on the design surface. To diagnose this failure, please try to run the project in a regular browser using the silverlight developer runtime. at

Microsoft.Windows.Design.Platform.SilverlightViewProducer.OnUnhandledException(Object sender, ViewUnhandledExceptionEventArgs e) at Microsoft.Expression.Platform.Silverlight.SilverlightPlatformSpecificView.OnUnhandledException(Object sender, ViewUnhandledExceptionEventArgs args) at Microsoft.Expression.Platform.Silverlight.Host.SilverlightImageHost.<>c_DisplayClass1.b_0(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

System.ArgumentException Value does not fall within the expected range. at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh) at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj) at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value) at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue) at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation) at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp) at System.Windows.Data.BindingExpression.SendDataToTarget() at System.Windows.Data.BindingExpression.SourceAcquired() at System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(Object sender, DataContextChangedEventArgs e) at System.Windows.Data.BindingExpression.DataContextChanged(Object sender, DataContextChangedEventArgs e) at System.Windows.DataContextChangedEventHandler.Invoke(Object sender, DataContextChangedEventArgs e) at System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e) at System.Windows.FrameworkElement.OnTreeParentUpdated(DependencyObject newParent, Boolean bIsNewParentAlive) at System.Windows.DependencyObject.UpdateTreeParent(IManagedPeer oldParent, IManagedPeer newParent, Boolean bIsNewParentAlive, Boolean keepReferenceToParent) at MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(IntPtr oldParentElement, IntPtr parentElement, IntPtr childElement, Byte bIsParentAlive, Byte bKeepReferenceToParent, Byte bCanCreateParent)
1

1 Answers

1
votes

I managed to replicate your issue but I receive different exception ("Element is already the child of another element"). I think the reason can be the same so see if it helps.

A little debugging shows that when SelectionMode is set to Multiple then the Header of GridViewSelectColumn becomes a CheckBox. This means that you're trying to add same CheckBox to both column header and CheckBox content inside your ListBox. Just try to remove SelectionMode and see if you have the same issue or not.

If that's the case then you can fix issue by adding a converter to CheckBox.Content binding that passes through only strings. For example:

public class HeaderConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
     if (value is string)
        return value;

     return string.Empty;
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
     throw new NotImplementedException();
  }
}

Then in XAML:

<ListBox ItemsSource="{Binding Columns, ElementName=WorklistGridView}">
    <ListBox.Resources>
        <local:HeaderConverter x:Key="headerConverter" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Header, Converter={StaticResource headerConverter}}"
                      IsChecked="{Binding IsVisible, Mode=TwoWay}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>