Why am getting a CS0103 error on GetValue/SetValue, but in another class it doesn't raise that error?

Rod At Work 866 Reputation points
2020-09-14T16:56:38.75+00:00

I'm trying to define a DependencyProperty in a viewmodel class. Here's what I've got:

public static readonly DependencyProperty SolutionIDProperty = DependencyProperty.Register("SolutionID", typeof(long), typeof(SolutionViewModel));
public long SolutionID
{
get { return (long)GetValue(SolutionIDProperty); }
set { SetValue(SolutionIDProperty, value); }
}

Both the GetValue() and SetValue() calls raise a CS0103 error. I'm including the System.Windows assembly and put a using statement at the top of the viewmodel class. GetValue() and SetValue() both are defined in DependencyObject class, which is in System.Windows. Like I said I've got the assembly referenced and included in the viewmodel's class at the top of the class.

What really confuses me is that a colleague of my is using the DependencyProperty in nearly the same way, and it works perfectly for him. Here's what he's got:

public static readonly DependencyProperty ButtonIndexProperty = DependencyProperty.Register("ButtonIndex", typeof(int), typeof(CoreButtonList));

public int ButtonIndex
{ get
{ return (int)GetValue(ButtonIndexProperty);
}
set
{ SetValue(ButtonIndexProperty, value);
}
}

His project includes the System.Windows assembly in the references, like mine does. And he's got a using statement at the code of the file, like mine done.

The only differences I can see are these:

  1. The variable names are different. Mine is SolutionID and his is ButtonIndex.
  2. I'm working with a C# long, he's working with a C# int.
  3. I'm trying to define a DependencyProperty in a ViewModel class. He defined a DependencyProperty in the code behind of a user control.

Other than those three differences, everything else is the same. I find it incredibly hard to believe that the problem is either #1 or #2, above. Could #3 be what's causing me to get a CS0103 error when I tried to build my solution? If so, how? Or is there something else going on?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,705 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 114K Reputation points
    2020-09-14T17:20:09.853+00:00

    Probably your class (model) is not inherited from DependencyObject, in contrast with the class where these functions work.

    Check some recommendations too:


  2. DaisyTian-1203 11,621 Reputation points
    2020-09-16T08:08:47.383+00:00

    I modify your SoultionID like :

    public static long GetSolutionID(DependencyObject obj)  
            {  
                return (long)obj.GetValue(SolutionIDProperty);  
            }  
      
            public static void SetSolutionID(DependencyObject obj, long value)  
            {  
                obj.SetValue(SolutionIDProperty, value);  
            }  
      
            public static readonly DependencyProperty SolutionIDProperty = DependencyProperty.Register("SolutionID", typeof(long), typeof(SolutionViewModel));  
    

    And use it in xaml like:

          <TextBlock Width="420" Height="150" TextWrapping="Wrap" Background="Azure"  local:SolutionViewModel.SolutionID="5" ></TextBlock>  
    

    It runs no error.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments