XAML editor just keeps showing: The member is not recognized or is not accessible
Asked Answered
B

3

5

I created an UserControl: A TextBox that shows an icon on the left side. My code seems to work but the following error keeps showing and makes the XAML editor mark it as an error.

The member is not recognized or is not accessible.

I also don't get autocompletion inside my properties at all, I assume it is because of the error? I'm not that familiar with WPF UserControls.

I have already cleaned and rebuilt / restarted my project multiple times. That didn't help, the XAML editor just keeps showing this error on every custom property.

I'm using .NET 4.6.1.

LoginWindow.xaml:

<Window x:Class="SMSBrowser.LoginWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:control="clr-namespace:SMSBrowser.Controls"
    Title="SMS Browser - Login" SizeToContent="WidthAndHeight" ResizeMode="NoResize" Background="DimGray">
<Grid>
    <StackPanel Margin="30" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="Test" TextAlignment="Center" FontSize="32" FontWeight="Bold" Foreground="White" Margin="0,0,0,25" />
        <control:IconTextBox CustomBackground="Yellow" CustomIconSource="..\Resources\Icons\User.ico" Height="25" Margin="0,0,0,4" />
        <control:IconTextBox CustomBackground="Red" CustomIconSource="..\Resources\Icons\Key.ico" Height="25" Margin="0,4,0,4" />
        <Button Content="Login" Height="25" Width="400" Margin="0,4,0,0" />
    </StackPanel>
</Grid>

IconTextBox.xaml

<UserControl x:Class="SMSBrowser.Controls.IconTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d"
         d:DesignHeight="25" d:DesignWidth="300"
         x:Name="LayoutRoot">
<Border BorderBrush="{Binding Path=CustomBorderBrush, ElementName=LayoutRoot}" BorderThickness="{Binding Path=CustomBorderThickness, ElementName=LayoutRoot}">
    <DockPanel Background="{Binding Path=CustomBackground, ElementName=LayoutRoot}">
        <Image Source="{Binding Path=CustomIconSource, ElementName=LayoutRoot}" Margin="{Binding Path=CustomIconMargin, ElementName=LayoutRoot}" DockPanel.Dock="Left" />
        <TextBox Text="{Binding Path=CustomText, ElementName=LayoutRoot}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Center" BorderThickness="0" />
    </DockPanel>
</Border>

IconTextBox.cs

namespace SMSBrowser.Controls
{
/// <summary>
/// Interaction logic for IconTextBox.xaml
/// </summary>
public partial class IconTextBox : UserControl
{
    public IconTextBox()
    {
        InitializeComponent();
        DataContext = LayoutRoot;
    }

    public string CustomBackground
    {
        get { return (string)GetValue(CustomBackgroundProperty); }
        set { SetValue(CustomBackgroundProperty, value); }
    }

    public static readonly DependencyProperty CustomBackgroundProperty =
        DependencyProperty.Register("CustomBackground", typeof(string), typeof(IconTextBox));

    public string CustomBorderBrush
    {
        get { return (string)GetValue(CustomBorderBrushProperty); }
        set { SetValue(CustomBorderBrushProperty, value); }
    }

    public static readonly DependencyProperty CustomBorderBrushProperty =
        DependencyProperty.Register("CustomBorderBrush", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));

    public string CustomBorderThickness
    {
        get { return (string)GetValue(CustomBorderThicknessProperty); }
        set { SetValue(CustomBorderThicknessProperty, value); }
    }

    public static readonly DependencyProperty CustomBorderThicknessProperty =
        DependencyProperty.Register("CustomBorderThickness", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));

    public string CustomIconMargin
    {
        get { return (string)GetValue(CustomIconMarginProperty); }
        set { SetValue(CustomIconMarginProperty, value); }
    }

    public static readonly DependencyProperty CustomIconMarginProperty =
        DependencyProperty.Register("CustomIconMargin", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));

    public string CustomIconSource
    {
        get { return (string)GetValue(CustomIconSourceProperty); }
        set { SetValue(CustomIconSourceProperty, value); }
    }

    public static readonly DependencyProperty CustomIconSourceProperty =
        DependencyProperty.Register("CustomIconSource", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));

    public string CustomText
    {
        get { return (string)GetValue(CustomTextProperty); }
        set { SetValue(CustomTextProperty, value); }
    }

    public static readonly DependencyProperty CustomTextProperty =
        DependencyProperty.Register("CustomText", typeof(string), typeof(IconTextBox), new PropertyMetadata(""));
}
}

Screenshot:

Errors

Braunschweig answered 28/1, 2016 at 15:11 Comment(7)
do you have your DataContext of your user control set?Benelux
In the constructor of IconTextBox it is set to LayoutRoot, I hope that's right.Braunschweig
try setting the datatype of your colors to Brush instead of stringBenelux
You mean I should set public string CustomBackground and it's setter to be / return a Brush instead of a string? I did that and the error still persists with that background value (and all the other values). If I also change the DP value to be typeof(Brush) it throws an error. System.ArgumentException: Default value type does not match type of property 'CustomBorderBrush'.Braunschweig
I've tried to duplicate your problem today but your code works just fine for me. I'm using VS2015 pro with .net 4.6.1. Not sure what to tell you at this point.Benelux
I added a screenshot, I still get the errors. :( Compiling works fine but the XAML editor is on error rampage.Braunschweig
I recently started having this problem as wellSpireme
B
8

I was able to replicate your problem and found a solution.
You need to add a default value for PropertyMetadata in CustomBackgroundProperty.

Try this

 public string CustomBackground {
        get { return (string)GetValue(CustomBackgroundProperty); }
        set { SetValue(CustomBackgroundProperty, value); }
 }

 public static readonly DependencyProperty CustomBackgroundProperty =  
        DependencyProperty.Register("CustomBackground", typeof(string),
        typeof(IconTextBox),new PropertyMetadata(null));
Benelux answered 29/1, 2016 at 16:51 Comment(0)
I
11

I had this problem. I eventually had to shutdown visual studio and restart it. Once I did that the XAML design time editor worked again.

Intercalation answered 24/7, 2018 at 21:1 Comment(2)
This just worked for me as well, so upvoted back from 0!Passant
Yeah had this issue referencing an enum in xaml for datatrigger value +1Pratincole
B
8

I was able to replicate your problem and found a solution.
You need to add a default value for PropertyMetadata in CustomBackgroundProperty.

Try this

 public string CustomBackground {
        get { return (string)GetValue(CustomBackgroundProperty); }
        set { SetValue(CustomBackgroundProperty, value); }
 }

 public static readonly DependencyProperty CustomBackgroundProperty =  
        DependencyProperty.Register("CustomBackground", typeof(string),
        typeof(IconTextBox),new PropertyMetadata(null));
Benelux answered 29/1, 2016 at 16:51 Comment(0)
S
0

On which line does the error show? I have tried your code using vs2010 and it worked without any error.

Statement answered 28/1, 2016 at 15:37 Comment(2)
If I declare my custom control without my custom properties, no errors are shown. But if I start using my custom properties, the property gets marked red and shows the error. Compiling and running works without errors though. I'm using vs2013 with all the latest updates.Braunschweig
Ok I have tried to set the Background of the textbox inside the usercontrol template to transparent and it worked without showing any exception (the custombackground property) I believe the reason is that Iam using an older .Net version hope somebody come with a new idiaStatement

© 2022 - 2024 — McMap. All rights reserved.