简体   繁体   中英

Getting access denied when raising property changed

I have a class defined like this (i have removed some code lines, like null checks, to keep it short):

public sealed class CodeTheme : INotifyPropertyChanged
{
    public void Reload()
    {
        PropertyChanged(sender, new PropertyChangedEventArgs("MyProperty"));
    }

    public Thickness MyProperty
    {
        get
        {
            return new Thickness():
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

I am registering the codetheme class in XAML like this, using the StandardStyles.xaml

<me:CodeTheme x:Key="Theming" />

Than, i am using it in varios templates and styles, like so:

<Style x:Key="Style1" TargetType="ListViewItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="Margin" Value="{Binding Source={StaticResource Theming}, Path=MyProperty}" />
</Style>

I then i might call the Reload method at any time in the program, the first time in the overwritten OnLaunched event of the App.xaml.cs

This works for colors, strings and other Thickness values when i assign them in DataTemplate . However when i use it in a Style , i get the following error as soon as i fire the PropertyChanged event:

System.UnauthorizedAccessException was unhandled by user code HResult=-2147024891 Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) Source=System StackTrace: (i have removed this) InnerException:

I have tried to raise the event by RunAsync from the Window.Current.Dispatcher but that didnt change anything. What am i doing wrong here?

上次我检查样式设置器中不支持绑定。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM