DataGrid does not work in partial trust xbap

Sep 15, 2008 at 10:05 PM
System.Security.SecurityException was unhandled by user code
Message="Request for the permission of type 'System.Security.Permissions.Securi
tyPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
Source="WPFToolkit"
StackTrace:
at Microsoft.Windows.Controls.WeakHashtable.SetWeak(Object key, Object value)
at Microsoft.Windows.Controls.DataGridHelper.GetPropertyTransferEnabledMapForObject(DependencyObject d, DependencyProperty p)
at Microsoft.Windows.Controls.DataGridHelper.TransferProperty(DependencyObject d, DependencyProperty p)
at Microsoft.Windows.Controls.DataGridColumn.SyncProperties()
at Microsoft.Windows.Controls.DataGrid.UpdateColumnSizeConstraints(IList list)
at Microsoft.Windows.Controls.DataGrid.OnColumnsChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at Microsoft.Windows.Controls.DataGridColumnCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
at Microsoft.Windows.Controls.DataGridColumnCollection.InsertItem(Int32 index, DataGridColumn item)
at System.Collections.ObjectModel.Collection`1.Add(T item)
at System.Collections.ObjectModel.Collection`1.System.Collections.IList.Add(Object value)
at System.Windows.Markup.BamlRecordReader.SetPropertyValueToParent(Boolean fromStartTag, Boolean& isMarkupExtension)
InnerException:

I managed to hack out the weak hash table references but then I lose the column headers. A workaround for this would be greatly appreciated and I would hope that the released version will support partial trust xbap as it is a supported means of deployment.
Sep 15, 2008 at 10:59 PM
I managed to find a fairly easy way to work around this issue.  I simply replaced the hash table with a generic collection.  Replace the following two methods and field declaration in DataGridHelper.cs (lines 354-387) with the ones below and everything should work :)
       
        private static Dictionary<DependencyProperty, bool> GetPropertyTransferEnabledMapForObject(DependencyObject d, DependencyProperty p)
        {
            Dictionary<DependencyProperty, bool> propertyTransferEnabledForObject = null;
            if (_propertyTransferEnabledMap.ContainsKey(d))
            {
                propertyTransferEnabledForObject = _propertyTransferEnabledMap[d];
            }
            if (propertyTransferEnabledForObject == null)
            {
                propertyTransferEnabledForObject = new Dictionary<DependencyProperty, bool>();
                _propertyTransferEnabledMap.Add(d, propertyTransferEnabledForObject);
            }

            return propertyTransferEnabledForObject;
        }

        private static bool IsPropertyTransferEnabled(DependencyObject d, DependencyProperty p)
        {
            Dictionary<DependencyProperty, bool> propertyTransferEnabledForObject = null;
            if (_propertyTransferEnabledMap.ContainsKey(d))
            {
                propertyTransferEnabledForObject = _propertyTransferEnabledMap[d];
            }

            if (propertyTransferEnabledForObject != null)
            {
                bool isPropertyTransferEnabled;
                if (propertyTransferEnabledForObject.TryGetValue(p, out isPropertyTransferEnabled))
                {
                    return isPropertyTransferEnabled;
                }
            }

            return false;
        }
       
        /// <summary>
        ///     Tracks which properties are currently being transfered.  This information is needed when GetPropertyTransferEnabledMapForObject
        ///     is called inside of Coersion.
        /// </summary>
        private static Dictionary<DependencyObject, Dictionary<DependencyProperty, bool>> _propertyTransferEnabledMap = new Dictionary<DependencyObject, Dictionary<DependencyProperty, bool>>();
Coordinator
Sep 16, 2008 at 2:14 PM
This is a known bug and will be fixed in the v1 release.